Question

I would like to do simple date calculations in Java. For example, compute the difference in days between to dates (having a 0 time component). Of course you could do a simple substraction of milliseconds, divided by the number of milliseconds per day, which works fine - until daylight saving times enter the scene. I am conscious that different interpretations of the "difference in days" are possible, in particuliar, whether one should take into account the time component or not. Let us assume we have a 0 time component for simplicity.

This kind of calculation seems to be a very common need, and I would find it nice to have a discussion on different approaches to the question.

Was it helpful?

Solution

I would have a look at the Joda date/time library, and in particular the ReadableInterval class.

Joda makes life a lot easier when manipulating dates/times in Java, and I believe it's the foundation of the new Java JSR 310 wrt. dates/times. i.e. it (or something very similar) will be present in a future version of Java.

OTHER TIPS

It looks like Date, Calendar, and DateUtils will do everything you're looking for. http://commons.apache.org/lang/api/org/apache/commons/lang/time/DateUtils.html

DateUtils allows truncating or rounding the time so you can just deal with only the dates. You also mentioned daylight savings time. The main problem is when someone says they did something at 01:30 on a day where there was DST change... which 01:30 was it? The first one or the second? Luckily this can be handled by the Calendar class since it stores the timezone and DST offset. For the specifics on all that, check this out: http://www.xmission.com/~goodhill/dates/deltaDates.html

The simpler option is to use the Days, Weeks, Months and Years classes in Joda.

That way, you can do something as simple as:

int daysDifference = Days.daysBetween(startInstant, endInstant).getDays();

Here is an article discussing the use of the Date/Calendar facilities in java to calculate elapsed time.

Using java.time

The modern approach is with the java.time classes, supplanting the troublesome old legacy date-time classes.

compute the difference in days between to dates (having a 0 time component)

The LocalDate class represents a date-only value without time-of-day and without time zone.

The ChronoUnit enum has some handy methods such as between.

In defining spans of time, the java.time classes use the Half-Open approach where the beginning is inclusive while the ending is exclusive.

LocalDate start = LocalDate.of( 2017 , Month.JANUARY , 23 );
LocalDate stop = LocalDate.of( 2017 , Month.FEBRUARY , 17 );
long daysBetween = ChronoUnit.DAYS.between( start , stop );

You can represent that span of time as an object, using the Period class.

Period p = Period.between( start , stop );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top