The LocalDateTime(Object instant, DateTimeZone zone) constructor ignores the timezone
See original GitHub issueKey information
-
Joda-Time version: 2.9.3
-
Result of
TimeZone.getDefault()
sun.util.calendar.ZoneInfo[id=“Europe/Athens”,offset=7200000,dstSavings=3600000,useDaylight=true,transitions=138,lastRule=java.util.SimpleTimeZone[id=Europe/Athens,offset=7200000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]] -
Result of
DateTimeZone.getDefault()
Europe/Athens
Problem description
The LocalDateTime(Object instant, DateTimeZone zone) constructor ignores the timezone due to iChronology = chronology.withUTC(); The somewhat analogue LocalDateTime(long instant, DateTimeZone zone) gives the answer considering the TimeZone.
Test case
new LocalDateTime(new LocalDateTime(2012, 1, 1, 0, 0), DateTimeZone.getDefault())
returns 2012-01-01T00:00:00.000
while…
new LocalDateTime(new LocalDateTime(2012, 1, 1, 0, 0).getLocalMillis(), DateTimeZone.getDefault())
returns 2012-01-01T02:00:00.000
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (3 by maintainers)
Huh, I thought you would never answer. But you did and you did it in a diplomatic way. Thank you for that!
I understand and I agree with you that, at this point, you can’t do anything about it. Given this, your suggestions here are welcomed! I’ll keep them in mind.
Have a great day forward!
I see the problem - you expect the new
LocalDateTime
to be affected by the time-zone - but I’m not sure its practical to do anything about it (other than to document the behaviour better). While withLocalDateTime
it might well be possible to use the time-zone and convert the local time in accordance with it, doing so for the same constructor onLocalDate
orLocalTime
would be confusing. In addition, since this constructor has worked this way for over 15 years, changing the behaviour now isn’t really viable. As such, the only way to achieve your goal would be to add a new factory method.This can also be worked around, along the lines of
base.toDateTime(DateTimeZone.UTC).withZone(DateTimeZone.forOffsetHours(8)).toLocalDateTime()
.Its also important to bear in mind that Joda-Time has been replaced by
java.time.*
in Java 8, so limited efforts now go into Joda-Time. Note thatjava.time
does not have theLocalDateTime
time-zone conversion you are trying to do either - the general approach is to useDateTime
in Joda-Time when using time-zones.