Tuesday, 15 March 2011

java - Time Zone off by 10 hours in Joda-Time -



java - Time Zone off by 10 hours in Joda-Time -

i need parse string joda-time datetime (or java.util.date.) illustration of string i'm 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 throws exception:

invalid format: "2013-02-07t16:05:54-0800" malformed @ "t04:03:20-0800"

so i'm parsing 't' out of there:

eventdatestr = eventdatestr.indexof("t") > 0 ? eventdatestr.replace("t", " ") : eventdatestr;

and trying again. time no exception time zone off:

2013-02-08t02:05:54.000+02:00

note difference: in original string timezone '-0800' , here it's '+02:00'. in turn changes entire date, day later.

what doing wrong?

call method withoffsetparsed on datetimeformatter object datetimeformatter keeps time zone parsed string, instead of offsetting local time zone.

regarding why t shown when print out datetime, basil bourque has nice explanation in comment below.

regarding t, datetime not string nor contain string. datetimeformatter instance can generate string representation of date, time, , time zone info stored within datetime. when invoke tostring method on datetime (either implicitly or explicitly), built-in formatter based on iso 8601 used automatically. formatter uses yyyy-mm-ddthh:mm:ss.ssssss+00:00 format.

java datetime timezone jodatime datetime-format

No comments:

Post a Comment