简体   繁体   中英

Java: Difference between two dates spanning over months

I have the following code that successfully gets me the difference between two days (in days, hours, minutes, seconds):

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
Date d1 = format.parse(startTime);
Date d2 = format.parse(endTime);
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.println("Start: " + startTime);
System.out.println("End: " + endTime);
System.out.println(diffDays + " days, " + diffHours + " hours, " + diffMinutes + " minutes, " + diffSeconds + " seconds");

However, this does not work when the dates cross into another month, for example:

Start: 2013-07-31 10:15:01
End: 2013-08-01 11:22:33
-29 days, -22 hours, -52 minutes, -28 seconds

Start: 2013-05-31 10:15:01
End: 2013-08-01 11:22:33
-29 days, -22 hours, -52 minutes, -28 seconds

Is it possible to intelligently span over months and get accurate time differences? I am familiar with Joda but would like to stick with standard Java APIs unless this is not possible without something like Joda.

While you should use Joda Time simply as a much better date/time API, I suspect the problem is actually just that you're parsing the values incorrectly. Use a format string of:

yyyy-MM-dd HH:mm:ss

... with dd instead of DD . The DD value is "day in year" which I suspect is confusing things, basically overriding the month part entirely. See the SimpleDateFormat documentation for more details.

You can validate what's wrong by printing out d1 and d2 after parsing...

You are using

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");

Should be

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

You'll get the correct result of

Start: 2013-05-31 10:15:01
End: 2013-08-01 11:22:33
62 days, 1 hours, 7 minutes, 32 seconds

Here's the javadoc for the date patterns. dd is day in month. DD is day in year. Your Date objects just weren't parsed the way you expected. Basically the day in year value of 31 was overwriting the month value.

Debuggers are your friend.

tl;dr

In Java 9 and later…

Duration.between(
    LocalDateTime.parse( "2013-07-31T10:15:01" ) ,
    LocalDateTime.parse( "2013-08-01T11:22:33" )
).getDaysPart()

…and…

.getHoursPart()
.getMinutesPart()
.getSecondsPart()

java.time

The other Answers are outdated. The Joda-Time project is now in maintenance mode, and advises migration to the java.time classes built into Java.

Period

The Duration class represents a span of time not attached to the timeline as a total number of seconds and nanoseconds. Such a span can be interpreted as a number of days, hours, minutes, and seconds assuming generic 24-hour days and 60-minute hours as no time zone is included.

LocalDateTime start = LocalDateTime.parse( "2013-07-31T10:15:01" );
LocalDateTime stop = LocalDateTime.parse( "2013-08-01T11:22:33" );
Duration d = Duration.between( start , stop );

span: 2013-07-31T10:15:01/2013-08-01T11:22:33

d.toString(): PT25H7M32S

These strings are in standard ISO 8601 formats. The PT25H7M32S string uses a format of PnYnMnDTnHnMnS where P marks the beginning and the T separates the years-months-days from hours-minutes-seconds. The string PT25H7M32S means “twenty-five hours, seven minutes, and thirty-two seconds”.

See that code live in IdeOne.com .

In Java 9 and later (but not Java 8), these Duration methods extract each part.

Given a duration of 49H30M20.123S…

  • toDaysPart() = 2
  • toHoursPart() = 1
  • toMinutesPart() = 30
  • toSecondsPart() = 20
  • toMillisPart() = 123
  • toNanosPart() = 123000000

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date , .Calendar , & java.text.SimpleDateFormat .

The Joda-Time project, now in maintenance mode , advises migration to java.time.

To learn more, see the Oracle Tutorial . And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use… ).

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 .

I think you should look at joda-time library http://joda-time.sourceforge.net/

Java default implementation of dates kind of suck, using joda time you can compute the intervals you want and its compatible with java Date class.

http://joda-time.sourceforge.net/quickstart.html (look at interval and time periods)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM