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.
You are using
Should be
You'll get the correct result of
Here's the javadoc for the date patterns.
dd
is day in month.DD
is day in year. YourDate
objects just weren't parsed the way you expected. Basically the day in year value of31
was overwriting the month value.Debuggers are your friend.
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:
... with
dd
instead ofDD
. TheDD
value is "day in year" which I suspect is confusing things, basically overriding the month part entirely. See theSimpleDateFormat
documentation for more details.You can validate what's wrong by printing out
d1
andd2
after parsing...tl;dr
In Java 9 and later…
…and…
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.These strings are in standard ISO 8601 formats. The
PT25H7M32S
string uses a format ofPnYnMnDTnHnMnS
whereP
marks the beginning and theT
separates the years-months-days from hours-minutes-seconds. The stringPT25H7M32S
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()
= 2toHoursPart()
= 1toMinutesPart()
= 30toSecondsPart()
= 20toMillisPart()
= 123toNanosPart()
= 123000000About 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)