jodatime - Joda DateTime output unexpected difference from gmt -
my code:
val pattern = "mm-dd-yy" val t = datetime.parse("07-01-86", datetimeformat.forpattern(pattern)).todatetime(datetimezone.forid("gmt")) val z = t.getmillis.asinstanceof[long] println("ms= "+z) // expected output: 520560000000 actual output: 520578000000
several online gmt date converters give different millis output datetime. know why?
in solution local time zone implicitly used when parsing date time. should use
val t = datetime.parse("07-01-86", datetimeformat.forpattern(pattern).withzoneutc())
to force datetime
created in utc zone. then, millis 520560000000
. no need execute todatetime(datetimezone)
on more.
otherwise, construction
val t = datetime.parse("07-01-86", datetimeformat.forpattern(pattern)).todatetime(datetimezone.forid("gmt"))
the datetime
first created in local tz (ie. midnight of 07-01-86 in your tz) , "cast" utc, preserving timestamp (ie. same timestamp, interpreted in utc, time part , day part change depending on local tz offset).
example (my tz +02:00):
datetime.parse("07-01-86", datetimeformat.forpattern(pattern)) // --> 1986-07-01 00:00 (+02:00) .todatetime(datetimezone.forid("gmt")) // --> 1986-06-30 22:00 (utc)
i assumed ok using utc
on gmt
there's also
datetimeformat.forpattern(pattern).withzone(...)
where can provide desired zone.
Comments
Post a Comment