Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

joda-time: new DateTime(String) vs DateTime.parse(String)

Using the joda-time-2.0 version library, I was wondering, which of this functions is better to construct from an ISO date (suposed XML xs:dateTime format): new DateTime(String) versus DateTime.parse(String).

Because both return a different result from same value. Example:

new DateTime("2012-08-16T07:22:05Z")
DateTime.parse("2012-08-16T07:22:05Z")

Resulting different because of the ISOChronology. First says is ISOChronology[Europe/Paris] and second ISOChronology[UTC], although milliseconds are the same.

Also, here recomends to use ISODateTimeFormat.dateTimeNoMillis(), giving the same result as using the first version (new).

like image 696
lucasvc Avatar asked Aug 30 '12 12:08

lucasvc


People also ask

Is Joda DateTime immutable?

Date and Time The many classes in Joda-Time are designed to allow the nuances of the domain to be fully expressed. The five date-time classes that will be used most are: Instant - Immutable class representing an instantaneous point on the time-line. DateTime - Immutable replacement for JDK Calendar.

How do I change the date format in Joda-time?

I think this will work, if you are using JodaTime: String strDateTime = "11/15/2013 08:00:00"; DateTime dateTime = DateTime. parse(strDateTime); DateTimeFormatter fmt = DateTimeFormat. forPattern("MM/dd/YYYY"); String strDateOnly = fmt.

Is Joda deprecated?

So the short answer to your question is: YES (deprecated).


1 Answers

The two methods use two different conversion methods: the constructor uses an instance of InstantConverter, which in case of strings is a StringConverter and which doesn't yet support reading the timezone from the passed string, while the parse method uses a DateTimeFormatter which knows how to parse the timezone.

Although both formats in theory accept an ISO datetime format, I consider that the constructor is buggy since it always uses the system timezone instead of the one specified in the string. This is inconsistent with the other possible values accepted by this constructor, which do take into account a chronology with its timezone offset. For example, this constructor will return a DateTime object with the UTC timezone:

new DateTime(DateTime.parse("2012-08-16T07:22:05Z"))
like image 84
Sergiu Dumitriu Avatar answered Nov 07 '22 04:11

Sergiu Dumitriu