Difference of two Joda LocalDateTimes

I have 2 Joda LocalDateTime LocalDateTime and you need to create a third one that represents the difference between them:

 LocalDateTime start = getStartLocalDateTime(); LocalDateTime end = getEndLocalDateTime(); LocalDateTime diff = ??? 

The only way I can understand is to painstakingly go through each date / time field and perform its corresponding minus operation:

 LocalDateTime diff = end; diff.minusYears(start.getYear()); diff.minusMonths(start.getMonthOfYear()); diff.minusDays(start.getDayOfMonth()); diff.minusHours(start.getHourOfDay()); diff.minusMinutes(start.getMinuteOfHour()); diff.minusSeconds(start.getSecondsOfMinute()); 

The end result would be to simply call the diff toString() method and get something meaningful. For example, if start.toString() produces 2012/02 / 08T15: 05: 00 and end.toString() produces 2012/02 / 08T16: 00: 00, then diff.toString() will be the difference (55 minutes) and might look like 2012/02 / 08T00: 55 :. 00

And if this is a terrible abuse of LocalDateTime , then I just need to know how to use the time difference between the two and split this difference into an easily readable (human-friendly) format.

Thanks in advance!

+8
java jodatime
source share
4 answers

You can use the org.joda.time.Period class for this. Read more about org.joda.time.Period

Example:

 LocalDateTime endOfMonth = now.dayOfMonth().withMaximumValue(); LocalDateTime firstOfMonth = now.dayOfMonth().withMinimumValue(); Period.fieldDifference(firstOfMonth, endOfMonth) 
+14
source share

Duration is better for some cases. You can get a "time-independent" duration for use with LocalDateTimes (in the local time line) with this trick:

 public static Duration getLocalDuration(LocalDateTime start, LocalDateTime end) { return new Duration(start.toDateTime(DateTimeZone.UTC), end.toDateTime(DateTimeZone.UTC)); } 
+8
source share

I found a workaround using the following steps:

  • Take two LocalDateTime objects as "Start and End"
  • Convert both of them to an Instant object, for example start.toInstant(ZoneOffset.UTC);
  • Calculate duration using Duration.between(instant t1, instant t2)
  • Convert it to nanoseconds or milliseconds or seconds using various conversion methods. Nanoseconds can be calculated as Duration.toNanos()

I gave a complete example below.

 public long getDuration(LocalDateTime start, LocalDateTime end) { //convert the LocalDateTime Object to an Instant Instant startInstant = start.toInstant(ZoneOffset.UTC); Instant endInstant = end.toInstant(ZoneOffset.UTC); //difference between two Instants is calculated //convert to nano seconds or milliseconds long duration=Duration.between(startInstant, endInstant).toNanos(); return duration; } 
+1
source share
 public static void printTimeDiffJoda(final String start, final String end) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { // Parse datetime string to org.joda.time.DateTime instance DateTime startDate = new DateTime(format.parse(start)); DateTime endDate = new DateTime(format.parse(end)); System.out.println("Joda Time API - Time Difference "); System.out.println("in years: " + Years.yearsBetween(startDate, endDate).getYears()); System.out.println("in months: " + Months.monthsBetween(startDate, endDate).getMonths()); System.out.println("in days: " + Days.daysBetween(startDate, endDate).getDays()); System.out.println("in hours: " + Hours.hoursBetween(startDate, endDate).getHours()); System.out.println("in minutes: " + Minutes.minutesBetween(startDate, endDate).getMinutes()); System.out.println("in seconds: " + Seconds.secondsBetween(startDate, endDate).getSeconds()); System.out.println(); } catch (ParseException e) { e.printStackTrace(); } } 

http://www.luyue.org/calculate-difference-between-two-dates/

0
source share

All Articles