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

时间:2021-10-18 19:12:49

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?

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

Thanks In advance

提前致谢

3 个解决方案

#1


63  

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

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

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

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

#2


399  

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

这可能是你所追求的更多

#3


-1  

Something like...

就像是...

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

#1


63  

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

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

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

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

#2


399  

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

这可能是你所追求的更多

#3


-1  

Something like...

就像是...

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