Calculating Duration Using Joda-Time

I am trying to use Joda-Time to find out the length of time between two time points where each point is indicated in its own local time zone.

eg.

DateTime ny = new DateTime(2011, 2, 2, 7, 0, 0, 0, DateTimeZone.forID("America/New_York")); DateTime la = new DateTime(2011, 2, 3, 10, 15, 0, 0, DateTimeZone.forID("America/Los_Angeles")); DateTime utc1 = ny.withZone(DateTimeZone.UTC); DateTime utc2 = la.withZone(DateTimeZone.UTC); Period period = new Period(utc1, utc2); 

Now I want to know if this takes into account daylight savings and leap years ... Also, is using the "Period" the right Joda-Time way to achieve this? Thanks;)

+4
source share
2 answers

The code you provided will work and take into account time zones, but you do not need to do the conversion to UTC. This code is simpler and does the same (using duration, not period):

 DateTime ny = new DateTime(2011, 2, 2, 7, 0, 0, 0, DateTimeZone.forID("America/New_York")); DateTime la = new DateTime(2011, 2, 3, 10, 15, 0, 0, DateTimeZone.forID("America/Los_Angeles")); Duration duration = new Interval(ny, la).toDuration(); 
+10
source

Depending on how you use it, the above code may not be a good idea.

All DateTime constructors that accept int for year / month / day / hour, etc., are vulnerable to Daylight Saving Time (DST) transitions, in which case Joda will throw an exception. So if the hour during the transition is a possible entry into your application, it will not work:

 DateTime ny = new DateTime(2011, 3, 13, 2, 0, 0, 0, DateTimeZone.forID("America/New_York")); Exception in thread "main" java.lang.IllegalArgumentException: Illegal instant due to time zone offset transition: 2011-03-13T07:00:00.000 at org.joda.time.chrono.ZonedChronology.localToUTC(ZonedChronology.java:143) at org.joda.time.chrono.ZonedChronology.getDateTimeMillis(ZonedChronology.java:119) at org.joda.time.chrono.AssembledChronology.getDateTimeMillis(AssembledChronology.java:133) at org.joda.time.base.BaseDateTime.<init>(BaseDateTime.java:254) at org.joda.time.base.BaseDateTime.<init>(BaseDateTime.java:223) at org.joda.time.DateTime.<init>(DateTime.java:264) 

Similarly, you will encounter another problem in the fall when it is not possible to determine what hour * refers to, since there will be 2 * 2 hours in this time zone. The DateTime withHourOfday and withTime methods are vulnerable to the same problem, as well as to parse datetimes as strings with time zones affected by DST.

Possible workarounds include

  • instantiation with any fixed offset time (e.g. UTC)
  • UTC timezone parsing
  • copying the actual time in the local time zone (for example, at midnight) and using plusHours to move forward to the desired time
  • have protection (if-statement) to protect against the second hour of the transition dates.
  • catch the exception and check when the next transition will happen (using DateTimeZone.nextTransition), and go back / forward accordingly.
+4
source

Source: https://habr.com/ru/post/1339535/


All Articles