6

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.

adarshr
  • 61,315
  • 23
  • 138
  • 167
Rakesh L
  • 1,136
  • 4
  • 18
  • 44

2 Answers2

12

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.");
JodaStephen
  • 60,927
  • 15
  • 95
  • 117
Ilya
  • 29,135
  • 19
  • 110
  • 158
0

In your SimpleDateFormat, you have HH to specify hours, and as stated in documentation, HH stands for - Hour in day (0-23). So AM/PM does not apply here. If you want AM/PM hours, you should use KK or hh instead. You can see documentation here.

Dmitry Kuskov
  • 1,001
  • 6
  • 10
  • I'm in huge turmoil .can you provide me a sample ? – Rakesh L Jul 06 '13 at 09:32
  • You can specify format as `SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");` Then you can enter dates like: `String dateStart = "01/14/2012 09:29:58 AM";` `String dateStop = "01/15/2012 10:31:48 PM";` – Dmitry Kuskov Jul 06 '13 at 12:50
  • sorry :( I did try this. I couldn't get the solution .The solution that Ilya gave is currently working for me... Anyway,thank you for trying to help me :) – Rakesh L Jul 09 '13 at 06:21