How to find difference between two Joda-Time DateTimes in minutes

Deepak Kumar picture Deepak Kumar · Oct 12, 2012 · Viewed 117.1k times · Source

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?

Answer

MadProgrammer picture MadProgrammer · Oct 12, 2012

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