tl;dr
ChronoUnit.DAYS.between( then , now )
Naïve calculations
You assume in your code that every day is exactly twenty four hours long. Not true. Anomalies such as Daylight Saving Time (DST) mean that days may vary such as 23 hours long, 25 hours, or other numbers. The very meaning of a time zone is to track a history of these anomalies.
Also, you assume the day starts at midnight. Not true. Because of anomalies, some days start at other time-of-day such as 01:00
.
Avoid legacy date-time classes
You are using the troublesome old date-time classes such as java.util.Date
, java.util.Calendar
, and java.text.SimpleTextFormat
are now legacy, supplanted by the java.time classes.
Time zone
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
Do not extend java.time
Do not extend (subclass) the java.time classes (they are marked final
).
And do not generalize to their interfaces for your business logic; stick to the concrete classes of this framework. While generalizing makes sense in other frameworks such as Java Collections, not so in java.time.
Using java.time
This work is much easier with the java.time classes.
ZonedDateTime
represents a moment on the timeline with an assigned tim zone (ZoneId
).
ZoneId z = ZoneId.of( "America/Montreal" );
// of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone)
ZonedDateTime then = ZonedDateTime.of( 2017 , 1 , 23 , 12 , 34 , 56 , 123456789 , z );
ZonedDateTime now = ZonedDateTime.now( z );
The ChronoUnit
enum can calculate elapsed time between a pair of moments.
long days = ChronoUnit.DAYS.between( then , now );
See this code run live at IdeOne.com.
then.toString(): 2017-01-23T12:34:56.123456789-05:00[America/Montreal]
now.toString(): 2017-03-01T21:26:04.884-05:00[America/Montreal]
days: 37
Converting legacy instances to java.time
If you are given GregorianCalendar
objects, convert to java.time using new methods added to the old classes.
ZonedDateTime zdt = myGregCal.toZonedDateTime() ;
If you know a Calendar
instance is actually a GregorianCalendar
, cast it.
GregorianCalendar myGregCal = (GregorianCalendar) myCal ;
Half-Open
The java.time classes define spans of time by the Half-Open approach where the beginning is inclusive while the ending is exclusive.
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?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
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.