Search code examples
javadatetimetimezonejodatimejava-time

For Some Timezones, Corrected Time after setting TimeZone via JodaTime and java.time are giving different results, Why?


Following is a small code snippet in java trying to convert a time in millis to a readable date time format,

Long timeInMillis=1615806808301l; //2021-03-15T16:43:28.301+05:30 IST

String timeZone="Europe/Istanbul";

MutableDateTime  mdateTime  = new MutableDateTime(timeInMills);
mdateTime.setZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone(timeZone)));

ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timeInMills), ZoneId.of(timeZone));

Following are the results given by the Joda[mdateTime] and Java.time[zdt] for the same timeInmillis and timezone,

Europe/Istanbul
2021-03-15T13:13:28.301+02:00[Europe/Istanbul]
2021-03-15T14:13:28.301+03:00[Europe/Istanbul]

Turkey
2021-03-15T13:13:28.301+02:00
2021-03-15T14:13:28.301+03:00[Turkey]

Europe/Moscow
2021-03-15T15:13:28.301+04:00
2021-03-15T14:13:28.301+03:00[Europe/Moscow]

Europe/Minsk
2021-03-15T14:13:28.301+03:00
2021-03-15T14:13:28.301+03:00[Europe/Minsk]

As you can see, For Some Timezones, the results are different,

PS: My actual Intention is not to convert the timeInmillis to a readable date time format, but to understand why the results are different.

PS: The System Timezone was IST[+05:30]


Solution

  • Based on the results you are showing for Joda-Time, you likely are using a very old version.

    Time zones change at the whim of governments. It's very important to always use the latest version and to stay on top of updates.

    Upgrade to the current version of Joda-Time (2.10.10 at the time of writing this) and the discrepancy you reported should go away.