Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeReview: java Dates diff (in day resolution)
    text
    copied!<p>Please your opinion on the following code.</p> <p>I need to calculate the diff in days between 2 Date objects. It is assured that both Date objects are within the same TimeZone.</p> <pre><code>public class DateUtils { public final static long DAY_TIME_IN_MILLIS = 24 * 60 * 60 * 1000; /** * Compare between 2 dates in day resolution. * * @return positive integer if date1 &gt; date2, negative if date1 &lt; date2. 0 if they are equal. */ public static int datesDiffInDays(final Date date1, final Date date2){ long date1DaysMS = date1.getTime() - (date1.getTime() % DAY_TIME_IN_MILLIS); long date2DaysMS = date2.getTime() - (date2.getTime() % DAY_TIME_IN_MILLIS); long timeInMillisDiff = (date1DaysMS - date2DaysMS); int ret = (int) (timeInMillisDiff / DAY_TIME_IN_MILLIS); return ret; } </code></pre> <p>Can you point to a problem that I might have missed ?</p> <p>EDIT: @mmyers asked if pass my unit test. Well - Yes. But I have no real experience with dates and I know that is a big subject. Posted below the unit test that I'm using.</p> <pre><code>public class TestMLDateUtils { @Test public final void testDatesDiffInDays() { TimeZone.setDefault(TimeZone.getTimeZone("UTC")); // 00:00:00.000 1.1.1970 Calendar cal1970 = Calendar.getInstance(); cal1970.setTimeInMillis(0); Calendar tested = Calendar.getInstance(); tested.setTimeInMillis(0); // Add 1 millisecond, date = 00:00:00.001 1.1.1970 tested.add(Calendar.MILLISECOND, 1); assertTrue(DateUtils.datesDiffInDays(cal1970.getTime(), tested.getTime()) == 0); // Add 1 second, date = 00:00:01.001 1.1.1970 tested.add(Calendar.SECOND, 1); assertTrue(DateUtils.datesDiffInDays(cal1970.getTime(), tested.getTime()) == 0); // Add 1 minute, date = 00:01:01.001 1.1.1970 tested.add(Calendar.MINUTE, 1); assertTrue(DateUtils.datesDiffInDays(cal1970.getTime(), tested.getTime()) == 0); // Add 1 hour, date = 01:01:01.001 1.1.1970 tested.add(Calendar.HOUR_OF_DAY, 1); assertTrue(DateUtils.datesDiffInDays(cal1970.getTime(), tested.getTime()) == 0); // date = 23:59:59.999 1.1.1970 tested.setTimeInMillis(0); tested.add(Calendar.MILLISECOND, 999); tested.add(Calendar.SECOND, 59); tested.add(Calendar.MINUTE, 59); tested.add(Calendar.HOUR_OF_DAY, 23); //System.out.println("D: " + tested.getTime()); assertTrue(DateUtils.datesDiffInDays(cal1970.getTime(), tested.getTime()) == 0); // date = 00:00:00.000 2.1.1970 tested.setTimeInMillis(0); tested.add(Calendar.DAY_OF_MONTH, 1); assertTrue(DateUtils.datesDiffInDays(cal1970.getTime(), tested.getTime()) == -1); assertTrue(DateUtils.datesDiffInDays(tested.getTime(), cal1970.getTime()) == 1); // date = 00:00:00.001 2.1.1970 tested.add(Calendar.MILLISECOND, 1); assertTrue(DateUtils.datesDiffInDays(cal1970.getTime(), tested.getTime()) == -1); assertTrue(DateUtils.datesDiffInDays(tested.getTime(), cal1970.getTime()) == 1); // date = 00:00:01.001 2.1.1970 tested.add(Calendar.SECOND, 1); assertTrue(DateUtils.datesDiffInDays(cal1970.getTime(), tested.getTime()) == -1); assertTrue(DateUtils.datesDiffInDays(tested.getTime(), cal1970.getTime()) == 1); // date = 00:01:01.001 2.1.1970 tested.add(Calendar.MINUTE, 1); assertTrue(DateUtils.datesDiffInDays(cal1970.getTime(), tested.getTime()) == -1); assertTrue(DateUtils.datesDiffInDays(tested.getTime(), cal1970.getTime()) == 1); // date = 01:01:01.001 2.1.1970 tested.add(Calendar.HOUR_OF_DAY, 1); assertTrue(DateUtils.datesDiffInDays(cal1970.getTime(), tested.getTime()) == -1); assertTrue(DateUtils.datesDiffInDays(tested.getTime(), cal1970.getTime()) == 1); // date = 13:01:01.001 2.1.1970 tested.add(Calendar.HOUR_OF_DAY, 12); assertTrue(DateUtils.datesDiffInDays(cal1970.getTime(), tested.getTime()) == -1); assertTrue(DateUtils.datesDiffInDays(tested.getTime(), cal1970.getTime()) == 1); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload