Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timer - How do I calculate the difference between two dates using Joda Time?

Tags:

java

jodatime

I want to get the difference between two times P (start time) and Q (end time) using Joda Time. P and Q could be times on different days or even on the same day. I want to get the difference in format HH-MM-SS, where H=hours, M=minutes, S=seconds.

I want to use this functionality in a timer. I assume that no one will use my timer to measure more than 24 hours.

Please guide me to do this.

like image 808
david blaine Avatar asked Sep 12 '12 23:09

david blaine


People also ask

Which method finds the difference in days between two dates?

Use the DATEDIF function when you want to calculate the difference between two dates.

What is the use of Joda-Time?

Joda-Time provides support for multiple calendar systems and the full range of time-zones. The Chronology and DateTimeZone classes provide this support. Joda-Time defaults to using the ISO calendar system, which is the de facto civil calendar used by the world.

What is Joda-Time API?

Joda-Time is an API created by joda.org which offers better classes and having efficient methods to handle date and time than classes from java. util package like Calendar, Gregorian Calendar, Date, etc. This API is included in Java 8.0 with the java.

How to create new DateTime in Java?

To create a datetime object representing a specific date and time, you may use an initialization string: DateTime dt = new DateTime("2004-12-13T21:39:45.618-08:00"); The initialization string must be in a format that is compatible with the ISO8601 standard.


2 Answers

Take a look at the Joda time FAQ http://joda-time.sourceforge.net/faq.html#datediff

And you can use a PeriodFormatter to get the format of your choice. Try the following sample code.

DateTime dt = new DateTime();
DateTime twoHoursLater = dt.plusHours(2).plusMinutes(10).plusSeconds(5);
Period period = new Period(dt, twoHoursLater);
PeriodFormatter HHMMSSFormater = new PeriodFormatterBuilder()
        .printZeroAlways()
        .minimumPrintedDigits(2)
        .appendHours().appendSeparator("-")
        .appendMinutes().appendSeparator("-")
        .appendSeconds()
        .toFormatter(); // produce thread-safe formatter
System.out.println(HHMMSSFormater.print(period));
like image 90
IceMan Avatar answered Oct 31 '22 13:10

IceMan


I admire the Joda date/time API. It offers some cool functionality and if I needed an immutable calendar or some of the esoteric calendar options it offers, I'd be all over it.

It is still an external API though.

So ... why use it when you don't have to. In the Joda API, the "Instant" is the exact same thing as a Java API "Date" (or pretty close to it). These are both thin wrappers around a long that represents an instant in POSIX EPOCH UTC time (which is the number of milliseconds that have elapsed since 00:00am Jan 1, 1970 UTC.

If you have either two Instants or two Dates, computing the days between them is trivial, and the Joda library is absolutely not needed for this purpose alone:

public double computeDaysBetweenDates(Date earlier, Date later) {
    long diff;

    diff = later.getTime() - earlier.getTime();
    return ((double) diff) / (86400.0 * 1000.0);
}

This assumes that the number of seconds in a day is 86400 ... and this is mostly true.

Once you have a difference as a double, it is trivial to convert the fractional portion of the answer (which is the fraction of one day) into HH:MM:SS.

like image 42
scottb Avatar answered Oct 31 '22 12:10

scottb