Joda-Time DateTime change timezone?

I am reading lines from a file that looks like "2015-06-06T01:51:49-06:00"in Joda-Time DateTime . But it DateTimedoes not behave the way I want.

import org.joda.time.DateTime;

System.out.println("2015-06-06T01:51:49-06:00");
System.out.println(new DateTime("2015-06-06T01:51:49-06:00"));

The result is

2015-06-06T01:51:49-06:00
2015-06-06T00:51:49.000-07:00

Later I need an hour and minutes. That will be 1:51. But DateTime prints it in a different time zone, I guess? How can I get a datetime for print2015-06-06T01:51:49.000-06:00

+4
source share
1 answer

A DateTimestores the time zone, but the constructor DateTime(Object instant)first converts Stringto the moment (millis), thereby losing time zone information, so it applies the default time zone to this moment.

, DateTime.parse(String str):

System.out.println("2015-06-06T01:51:49-06:00");
System.out.println(new DateTime("2015-06-06T01:51:49-06:00"));
System.out.println(DateTime.parse("2015-06-06T01:51:49-06:00"));

2015-06-06T01:51:49-06:00
2015-06-06T03:51:49.000-04:00
2015-06-06T01:51:49.000-06:00
+6

Source: https://habr.com/ru/post/1616451/


All Articles