如何找出两个Joda时间之间的差值(以分钟为单位)

2022-08-31 07:41:20

以下是我写的方法:

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

现在从我的方法中,您可以看到我在名为的对象中有一个Joda-Time DateTime对象,从我的结果中,我得到了一个时间戳,我正在将其转换为jodatime。现在我想找出这两个日期之间的差异,我该怎么做?datetimepunchTime


答案 1

像这样...

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

这将以毫秒为单位获得两个 DateTime 对象之间的差异:

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

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

推荐