question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The LocalDateTime(Object instant, DateTimeZone zone) constructor ignores the timezone

See original GitHub issue

Key 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:closed
  • Created 6 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
veveronecommented, May 8, 2018

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!

1reaction
jodastephencommented, May 8, 2018

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 with LocalDateTime 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 on LocalDate or LocalTime 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 that java.time does not have the LocalDateTime time-zone conversion you are trying to do either - the general approach is to use DateTime in Joda-Time when using time-zones.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Purpose of Joda Time LocalDate(Object instant ...
LocalDate is an immutable datetime class representing a date without a time zone. Yet there is a LocalDate(Object instant, DateTimeZone zone) ...
Read more >
LocalDateTime (Joda-Time 2.12.1 API)
Once the constructor is completed, the zone is no longer used. If the instant contains a chronology, it will be ignored. For example,...
Read more >
Set the Time Zone of a Date in Java - Baeldung
The Date class (which represents a specific instant in time) doesn't contain any time zone information. First, let's get the current UTC date ......
Read more >
ZonedDateTime (Java Platform SE 8 ) - Oracle Help Center
The local date-time is resolved to a single instant on the time-line. This is achieved by finding a valid offset from UTC/Greenwich for...
Read more >
LocalDateTime ("The Adobe AEM Quickstart and Web ...
Constructs an instance set to the local time defined by the specified instant evaluated using ISO chronology in the specified zone. If the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found