Reputation: 11187
I am reading strings from a file that look like "2015-06-06T01:51:49-06:00"
into a Joda-Time DateTime
object. But DateTime
isn't behaving 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 the following
2015-06-06T01:51:49-06:00
2015-06-06T00:51:49.000-07:00
Later on I need the hour and minutes. Here that would be 1:51. But DateTime is printing it out in a different timezone I'm guessing? How can I get DateTime to print out 2015-06-06T01:51:49.000-06:00
Upvotes: 4
Views: 319
Reputation: 159086
A DateTime
stores a time zone, but the DateTime(Object instant)
constructor first converts the String
to an instant (millis), thereby losing the timezone information, so it applies the default time zone to that instant.
To retain time zone, use 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"));
Output
2015-06-06T01:51:49-06:00
2015-06-06T03:51:49.000-04:00
2015-06-06T01:51:49.000-06:00
Upvotes: 6