Initialize LocalDateTime - with zero timings

Which is the right way to Initalize LocalDateTime - with zero timings

I'm using a method withTime(0, 0, 0, 0) implementation would look like new LocalDateTime().withTime(0, 0, 0, 0);,

So my question is when dealing with DayLight Savings or Timezone anamolies would this way of initialization cause any issue. Or Is there any other options to avoid those issues.

Thanks in advance

Upvotes: 3

Views: 7322

Answers (1)

Meno Hochschild
Meno Hochschild

Reputation: 44061

If you stay within the scope of LocalDateTime then you will not get any problem with daylight savings or other timezone transitions. A LocalDateTime has no knowledge about timezone-related things. BUT:

If you convert any LocalDateTime to a DateTime-instance which has a reference to the global timeline then you can get a problem. Descriptions of the associated problems can be found in the javadoc. The reasons for the deprecation of some methods are also explained (Brazil usually starts summer time at midnight so the creation of a LocalDateTime at midnight is not a problem but the creation of a DateTime).

If you want to know how to safely convert a LocalDateTime (with any time component possibly falling in a gap) to a (global) DateTime then Joda-Time does not offer any conflict strategy, unfortunately. The transition strategies Joda-Time applies are fixed and not configurable:

When the time zone is applied, the local date-time may be affected by daylight saving. In a daylight saving gap, when the local time does not exist, this method will throw an exception. In a daylight saving overlap, when the same local time occurs twice, this method returns the first occurrence of the local time.

However, some workaround for this limitation might be possible. You can for example ask the involved DateTimeZone if a given LocalDateTime falls in a gap. Dependent on the answer of this query you can decide which offset you want to apply in order to convert to a DateTime.

Upvotes: 1

Related Questions