Simple Java Date Calculations
Asked Answered
W

5

7

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.

Washy answered 13/3, 2009 at 16:1 Comment(2)
JODA TIME!!!!!! (In before the rush!)Zeringue
java.time!!!!!!Publish
M
11

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.

Magner answered 13/3, 2009 at 16:8 Comment(1)
FYI, the Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. See Tutorial by Oracle.Publish
A
3

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

Alginate answered 14/3, 2009 at 18:37 Comment(0)
H
2

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();
Helminthic answered 19/4, 2011 at 5:39 Comment(2)
And to convert your existing java.util.Date objects to ReadableIntervals that you can pass to daysBetween, you can do: new DateTime(somejavautildate)Christianson
FYI, the Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. See Tutorial by Oracle.Publish
P
2

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.

Publish answered 28/4, 2017 at 8:27 Comment(0)
N
1

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

Narwhal answered 13/3, 2009 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.