Java-How to calculate exact time difference when using Joda Time Jar

I am having a problem with the Joda time gang 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 am facing a problem with this time difference.

 01/14/2012 09:29:58 01/15/2012 10:31:48 

here are two points, but no indication of a period such as AM or PM. those. time can be in the morning or in the evening. How do I get the exact time difference?

Any help on this would be helpful.

+6
source share
2 answers

Using Formatting Jodes (Not 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 Formatting (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 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."); 
+12
source

In your SimpleDateFormat, you have HH to indicate the clock, and as indicated in the documentation, HH means - Hour in day (0-23). Therefore, AM / PM is not applicable here. If you need an AM / PM watch, you should use KK or hh instead. You can see the documentation here .

0
source

Source: https://habr.com/ru/post/948830/


All Articles