如何找到两个Joda-Time DateTimes之间的差异,以分钟为单位

问题

以下是我写的方法:

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;
}

现在从我的方法你可以看到我在对象名为datetime的aJoda-TimeDateTime对象,从我的结果我得到一个时间戳,我转换为jodatimepunchTime。现在我想找出这两个日期之间的差异,我该怎么做?

提前致谢


#1 热门回答(405 赞)

就像是...

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());

哪个输出

1
24
1440

要么

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

这可能是你所追求的更多


#2 热门回答(64 赞)

这将使你获得两个DateTime对象之间的区别,以毫秒为单位:

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

long diffInMillis = d2.getMillis() - d1.getMillis();

#3 热门回答(-1 赞)

就像是...

Minutes.minutesBetween(getStart(), getEnd()).getMinutes();