Calculating difference in dates in Java

2019-01-03 03:37发布

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.).

7条回答
Root(大扎)
2楼-- · 2019-01-03 03:53

With the date4j library:

int numDaysBetween = oneDate.numDaysFrom(anotherDate);
查看更多
看我几分像从前
3楼-- · 2019-01-03 03:54

I disagree with the claim that Java doesn't have a mechanism for calculating the difference between dates.

Java was designed for global use. It was designed so that there isn't a concept of date, there is only a concept of "time in milliseconds". Any interpretation of such a universal time as the time-and-date in a specific location under a specific convention is merely a projection or a view.

The calendar class is used to turn this sort of absolute time into dates. You can also add or subtract date components, if you really need to. The only way to provide a difference in term of components between two times would be Calendar generated and specific. Thus, you could argue that the standard library does not include a smart enough Gregorian Calendar, and I would agree that it leaves some to be desired.

That being said, there are numerous implementations of this kind of functionality, I see others have provided examples.

查看更多
▲ chillily
4楼-- · 2019-01-03 04:05

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.

查看更多
地球回转人心会变
5楼-- · 2019-01-03 04:05

Java's implementation of dates is poor. If you find Joda-Time too complicated, try my little contribution to open source: http://calendardate.sourceforge.net/javadoc/index.html

查看更多
The star\"
6楼-- · 2019-01-03 04:09

Java's not missing much, if you look at open source: try Joda-Time.

查看更多
甜甜的少女心
7楼-- · 2019-01-03 04:11

There is simple way to implement it. We can use Calendar.add method with loop. For example as below,

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

Date beginDate = dateFormat.parse("2013-11-29");
Date endDate = dateFormat.parse("2013-12-4");

Calendar beginCalendar = Calendar.getInstance();
beginCalendar.setTime(beginDate);

Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(endDate);

The minus days between beginDate and endDate, and the code as below,

int minusDays = 0;
while (true) {
  minusDays++;

  // Day increasing by 1
  beginCalendar.add(Calendar.DAY_OF_MONTH, 1);

  if (dateFormat.format(beginCalendar.getTime()).
            equals(dateFormat.format(endCalendar).getTime())) {
    break;
  }
}
System.out.println("The substractation between two days is " + (minusDays + 1));

Have Fun! @.@

查看更多
登录 后发表回答