This snippet of code always parses the date into the current timezone, and not into the timezone in the string being parsed.
final DateTimeFormatter df = DateTimeFormat .forPattern("EEE MMM dd HH:mm:ss 'GMT'Z yyyy"); final DateTime dateTime = df .parseDateTime("Mon Aug 24 12:36:46 GMT+1000 2009"); System.out.println("dateTime = " + dateTime); // outputs dateTime = 2009-08-24T04:36:46.000+02:00
It outputs:
dateTime = 2009-08-24T04:36:46.000+02:00
whereas I expect:
dateTime = 2009-08-24T04:36:46.000+10:00
Any ideas what I'm doing wrong?
Adjusting Time ZoneUse the DateTimeZone class in Joda-Time to adjust to a desired time zone. Joda-Time uses immutable objects. So rather than change the time zone ("mutate"), we instantiate a new DateTime object based on the old but with the desired difference (some other time zone). Use proper time zone names.
How to change the SimpleDateFormat to jodatime? String s = "2014-01-15T14:23:50.026"; DateTimeFormatter dtf = DateTimeFormat. forPattern("yyyy-MM-dd'T'HH:mm:ss. SSSS"); DateTime instant = dtf.
Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters. Mid level - pattern-based, like SimpleDateFormat.
So the short answer to your question is: YES (deprecated).
OK, further Googling gave me the answer to my own question: use withOffsetParsed()
, as so:
final DateTimeFormatter df = DateTimeFormat .forPattern("EEE MMM dd HH:mm:ss 'GMT'Z yyyy"); final DateTime dateTime = df.withOffsetParsed() .parseDateTime("Mon Aug 24 12:36:46 GMT+1000 2009");
This works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With