In joda time how to convert time zone without changing time
If your timestamp is: 2015-01-01T00:00:00.000-0500 (this is local time [for me])
Try this:
DateTime localDt = new DateTime(timestamp.getTime())
.withZoneRetainFields(DateTimeZone.UTC)
.withZone(DateTimeZone.getDefault());
2014-12-31T19:00:00.000-05:00
Breaking it down: This gives you a DateTime corresponding to your timestamp, specifying that it is in UTC:
new DateTime(timestamp.getTime())
.withZoneRetainFields(DateTimeZone.UTC)
2015-01-01T00:00:00.000Z
This gives you a DateTime but with the time converted to your local time:
new DateTime(timestamp.getTime())
.withZoneRetainFields(DateTimeZone.UTC)
.withZone(DateTimeZone.getDefault());
2014-12-31T19:00:00.000-05:00
You can use the withZoneRetainFields()
method of DateTime
to alter the timezone without altering the numerals in the date.
You can use class LocalDateTime
LocalDateTime dt = new LocalDateTime(t.getTime());
and convert LocalDateTime
to DateTime
DateTime dt = new LocalDateTime(timestamp.getTime()).toDateTime(DateTimeZone.UTC);
Joda DateTime
treats any time in millis like "millis since 1970y in current time zone". So, when you create DateTime
instance, it is created with current time zone.