Calculating difference in dates in Java

倾然丶 夕夏残阳落幕 提交于 2019-11-26 02:59:03

问题


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


回答1:


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.




回答2:


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




回答3:


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.




回答4:


With the date4j library:

int numDaysBetween = oneDate.numDaysFrom(anotherDate);



回答5:


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! @.@




回答6:


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.




回答7:


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



来源:https://stackoverflow.com/questions/453388/calculating-difference-in-dates-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!