Time zone off by 10 o'clock in Joda-Time
I need to parse a string into Joda-Time DateTime (or java.util.Date.) This is an example of the string I am getting:
eventDateStr = 2013-02-07T16:05:54-0800
The code I'm using:
DateTimeFormatter presentation = DateTimeFormat.forPattern("yyyy-MM-dd kk:mm:ssZ");
DateTime eveDate = presentation.parseDateTime(eventDateStr);
The above exception:
Invalid format: "2013-02-07T16:05:54-0800" is malformed at "T04:03:20-0800"
So, I parse the "T" from there:
eventDateStr = eventDateStr.indexOf("T") > 0 ? eventDateStr.replace("T", " ") : eventDateStr;
and try again. This time no exception, except for the time zone:
2013-02-08T02:05:54.000+02:00
Pay attention to the difference: in the original line, the time zone is "-0800", but here "+02: 00". This, in turn, changes the entire date, which is now the next day.
What am I doing wrong?
source to share
Call a method on the withOffsetParsed
object DateTimeFormatter
to get DateTimeFormatter
that stores the timezone processed from the string, instead of offsetting it to the local timezone.
As to why it is T
displayed when you print DateTime
, Basil Bourque , there is a nice explanation in the comment below.
Relatively,
T
a isDateTime
not a string and does not contain a string. The instanceDateTimeFormatter
can generate a string representation of the date, time, and time zone information stored inDateTime
. When you call a methodtoString
onDateTime
( implicitly or explicitly ), the built-in formatter based on ISO 8601 is used automatically. This formatter uses the formatYYYY-MM-DDTHH:MM:SS.ssssss+00:00
.
source to share