According to the JodaTime Javadoc, plusHours and plusDays both handle DST adjustments when adding time to a date. However, from my tests adding a days worth of hours vs adding 1 day gives a different result. Can someone explain to me why the following code gives the output below?
Code:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = format.parse("2014-11-01T02:00:00.000-0700");
System.out.println("origDate: " + date);
System.out.println("plusDays: " + new DateTime(date).plusDays(1).toDate());
System.out.println("plusHours: " + new DateTime(date).plusHours(24).toDate());
Output:
origDate: Sat Nov 01 02:00:00 PDT 2014
plusDays: Sun Nov 02 02:00:00 PST 2014
plusHours: Sun Nov 02 01:00:00 PST 2014
Not all local days have 24 hours. Assuming you are working in the US Pacific time zone, there are 25 hours on that particular day, due to the fall-back daylight saving time transition.
Calling
plusHours(24)
adds an exact duration of 24 hours elapsed time.Calling
plusDays(1)
increments the calendar day, even if the day is not exactly 24 hours.