153

Below is the method I wrote:

public List<Map<String, Object>> loadNotYetInEmployee(int shift, Date date,
        int transitionVal, String type, User user) {

    DateTime datetime = new DateTime(date);
    datetime = datetime
            .plus(Period.minutes(shiftTiming.getSession1InTime()));

    List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();

    sql = SqlMapUtils.getSql("attendance.attendancestatus.latein",
            parameters);
    result = getJdbcTemplate().queryForList(sql);
    for (int i = 0; i < result.size(); i++) {
        Date punchInTime = (Date) result.get(i).get("punchtime");
        DateTime punchTime = new DateTime(punchInTime);
    }
    return result;
}

Now from my method you can see I have a Joda-Time DateTime object in object named datetime and from my result I am getting one timestamp which I am converting to jodatime punchTime. Now I want to find out the diff between these two dates, how do I do that?

jarrodwhitley
  • 826
  • 10
  • 29
Deepak Kumar
  • 2,317
  • 5
  • 23
  • 21

4 Answers4

481

Something like...

DateTime today = new DateTime();
DateTime yesterday = today.minusDays(1);

Duration duration = new Duration(yesterday, today);
System.out.println(duration.getStandardDays());
System.out.println(duration.getStandardHours());
System.out.println(duration.getStandardMinutes());

Which outputs

1
24
1440

or

System.out.println(Minutes.minutesBetween(yesterday, today).getMinutes());

Which is probably more what you're after

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Does the above take into account time zones? Or should I convert to UTC / GMT first? – Doo Dah Apr 21 '16 at 01:15
  • @DooDah That's a good question, I "believe" that it does (as JodaTime has the concept of `LocalTime`), but I would suggest you test it and see. The question would be though, in which time zone would the resulting value be represented in? – MadProgrammer Apr 21 '16 at 01:24
  • I will test and figure this out. For me, the resulting timezone will likely be the server the code is running on. I am implementing a scheduled queue and will need to compute the amount of time between now and some future time based on a date from another time zone. I will report back what I find. – Doo Dah Apr 21 '16 at 01:29
  • 2
    I agree, this should have been the selected answer. Its neat and simple. – Sid Jan 09 '18 at 12:12
  • Creating a new Duration object just to get a time difference just isn't cool. Imagine having to perform a time difference on a large array of date/time objects. – Johann Jun 16 '18 at 07:20
  • @AndroidDev Given the fact that it's more accurate then "manual" calculating the result, I would say it's a better solution, besides if it's "really uncool", you could just do `long diffInMinutes = ChronoUnit.MINUTES.between(fromdate, todate);` and not care – MadProgrammer Jun 16 '18 at 08:34
  • Lemme slip in `Days.daysBetween(d1, d2)` which might be helpful to some. – Shubham Chaudhary Jun 18 '19 at 19:22
84

This will get you the difference between two DateTime objects in milliseconds:

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

long diffInMillis = d2.getMillis() - d1.getMillis();
alfredaday
  • 2,048
  • 18
  • 14
11

Something like...

Minutes.minutesBetween(getStart(), getEnd()).getMinutes();
Carlos Chaguendo
  • 2,895
  • 19
  • 24
6
DateTime d1 = ...;
DateTime d2 = ...;
Period period = new Period(d1, d2, PeriodType.minutes());
int differenceMinutes = period.getMinutes();

In practice I think this will always give the same result as the answer based on Duration. For a different time unit than minutes, though, it might be more correct. For example there are 365 days from 2016/2/2 to 2017/2/1, but actually it's less than 1 year and should truncate to 0 years if you use PeriodType.years().

In theory the same could happen for minutes because of leap seconds, but Joda doesn't support leap seconds.

morningstar
  • 8,952
  • 6
  • 31
  • 42