Calculating difference in dates in Java

I find it funny that Java (or the java.util library) does not have a built-in function to calculate difference in dates. I want to subtract one date from another to get the elapsed time between them. What is the best way to do this?

I know the simple way is to take the difference of the time in milliseconds and then convert that into days. However, I wanted to know if this works in all cases (with daylight saving, etc.).


I know the simple way is to take the difference of the time in milliseconds and then convert that into days. However, i wanted to know if this works in all cases (with daylight saving, etc.).

If your times are derived from UTC dates, or they are just the difference between two calls to System.getCurrentTimeMillis() measured on the same system, you will get a valid number of milliseconds as the difference, independent of any timezone issues. (which is why everything should be using UTC as a storage format -- it's much easier to go from UTC->local time; if you try to go the other way then you need to store the local timezone along with the local time -- or attempt to infer it, gack!)

As for turning this into a number of days, you should just be able to divide by 86400000... with the caveat that there is an occasional leap second every other year or so.


如果你看看开源的话,Java不会错过很多东西:尝试Joda-Time。


Use either Joda-Time or the new java.time package in Java 8.

Both frameworks use the Half-Open approach where the beginning is inclusive while the ending is exclusive. Sometimes notated as [) . This is generally the best approach for defining spans of time.

java.time

The java.time framework built into Java 8 and later has a Period class to represent a span of time as a number of years, a number of months, and a number of days. But this class is limited to whole days, no representation of hours, minutes, and seconds.

Note that we specify a time zone, crucial for determining a date. For example, a new day dawns earlier in Paris than in Montréal.

ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalDate now = LocalDate.now( zoneId );
LocalDate then = LocalDate.of( 2001, 1, 1 );
Period period = Period.between( then, now );

Then: 2001-01-01. Now: 2015-09-07. Period: P14Y8M6D. Days: 5362

For whole days, then Daylight Saving Time (DST) is irrelevant.

If you want a count of total days, use the ChronoUnit enum which includes some calculation methods. Notice the calculations return a long.

long days = ChronoUnit.DAYS.between( then, now );  // "5362" seen above.

I have asked about doing a full period in java.time, including hours, minutes, seconds. Not possible as of Java 8. A surprising workaround using the bundled libraries was suggested by Meno Hochschild: Use a Duration class found in the javax.xml.datatype package.

Joda-Time

Here is some example code in Joda-Time 2.3.

DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
DateTime start = new DateTime( 2014, 1, 2, 3, 4, 5, timeZone );
DateTime stop = new DateTime( 2014, 5, 2, 3, 4, 5, timeZone );
Period period = new Period( start, stop );

Calling toString will get you a string representation in the form defined by the ISO 8601 standard, PnYnMnDTnHnMnS .

链接地址: http://www.djcxy.com/p/18592.html

上一篇: 你如何减去Java中的日期?

下一篇: 计算Java中的日期差异