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?

+3


source to share


1 answer


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 is DateTime

not a string and does not contain a string. The instance DateTimeFormatter

can generate a string representation of the date, time, and time zone information stored in DateTime

. When you call a method toString

on DateTime

( implicitly or explicitly ), the built-in formatter based on ISO 8601 is used automatically. This formatter uses the format YYYY-MM-DDTHH:MM:SS.ssssss+00:00

.

+5


source







All Articles