Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java-How to calculate accurate time difference while using Joda Time Jar

I'm having problem with Joda time jar that I downloaded from http://sourceforge.net/projects/joda-time/files/joda-time/2.2/ . I can get the result when I use the following snippet

static void timeDifferencewithJoda()  {

    String dateStart = "01/14/2012 09:29:58";
    String dateStop = "01/15/2012 10:31:48";

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

    Date d1 = null;
    Date d2 = null;

    try {
        d1 = format.parse(dateStart);
        d2 = format.parse(dateStop);

        DateTime dt1 = new DateTime(d1);
        DateTime dt2 = new DateTime(d2);

        System.out.print(Days.daysBetween(dt1, dt2).getDays() + " days, ");
        System.out.print(Hours.hoursBetween(dt1, dt2).getHours() % 24 + " hours, ");
        System.out.print(Minutes.minutesBetween(dt1, dt2).getMinutes() % 60 + " minutes, ");
        System.out.print(Seconds.secondsBetween(dt1, dt2).getSeconds() % 60 + " seconds.");

     } catch (Exception e) {
        e.printStackTrace();
     }

}

The Result was

1 days, 1 hours, 1 minutes, 50 seconds.

But I face the confliction with this time difference .

01/14/2012 09:29:58

01/15/2012 10:31:48

here the two time has been declared ,but there's no indication to the period like AM or PM. ie., The time may be morning or evening .How to I get exact time difference ?

Any help regarding this will be useful.

like image 916
Rakesh L Avatar asked Jul 06 '13 05:07

Rakesh L


People also ask

How do you calculate duration in Java?

Find the time difference between two dates in millisecondes by using the method getTime() in Java as d2. getTime() – d1. getTime(). Use date-time mathematical formula to find the difference between two dates.

Does Joda datetime have TimeZone?

An interval in Joda-Time represents an interval of time from one instant to another instant. Both instants are fully specified instants in the datetime continuum, complete with time zone.

What is the replacement of Joda-Time?

Correct Option: D. In java 8,we are asked to migrate to java. time (JSR-310) which is a core part of the JDK which replaces joda library project.

Is Joda-Time followed in Java 8?

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.


1 Answers

Using Joda formatter (non AM/PM)

  final String dateStart = "01/14/2012 09:29:58";
  final String dateStop = "01/15/2012 10:31:48";
  final DateTimeFormatter format = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
  final DateTime dt1 = format.parseDateTime(dateStart);
  final DateTime dt2 = format.parseDateTime(dateStop);

  System.out.print(Days.daysBetween(dt1, dt2).getDays() + " days, ");
  System.out.print(Hours.hoursBetween(dt1, dt2).getHours() % 24 + " hours, ");
  System.out.print(Minutes.minutesBetween(dt1, dt2).getMinutes() % 60 + " minutes, ");
  System.out.print(Seconds.secondsBetween(dt1, dt2).getSeconds() % 60 + " seconds.");    

Using Joda formatter (with AM/PM)

  final String dateStart = "01/14/2012 09:29:58 AM";
  final String dateStop = "01/15/2012 10:31:48 PM";
  final DateTimeFormatter format = DateTimeFormat.forPattern("MM/dd/yyyy hh:mm:ss a");
  final DateTime dt1 = format.parseDateTime(dateStart);
  final DateTime dt2 = format.parseDateTime(dateStop);

  System.out.print(Days.daysBetween(dt1, dt2).getDays() + " days, ");
  System.out.print(Hours.hoursBetween(dt1, dt2).getHours() % 24 + " hours, ");
  System.out.print(Minutes.minutesBetween(dt1, dt2).getMinutes() % 60 + " minutes, ");
  System.out.print(Seconds.secondsBetween(dt1, dt2).getSeconds() % 60 + " seconds.");  

Calculate units between with Period

  final Period period = new Period(dt1, dt2);
  System.out.print(period.getDays() + " days, ");
  System.out.print(period.getHours() + " hours, ");
  System.out.print(period.getMinutes() + " minutes, ");
  System.out.print(period.getSeconds() + " seconds.");
like image 157
Ilya Avatar answered Oct 31 '22 14:10

Ilya