简体   繁体   English

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

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

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 . 现在从我的方法你可以看到我在一个名为datetime对象中有一个Joda-Time DateTime对象,从我的结果我得到一个时间戳,我转换为jodatime punchTime Now I want to find out the diff between these two dates, how do I do that? 现在我想找出这两个日期之间的差异,我该怎么做?

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 这可能是你所追求的更多

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

就像是...

Minutes.minutesBetween(getStart(), getEnd()).getMinutes();
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 . 在实践中,我认为这总是会给出与基于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() . 例如,从2016/2/2到2017/2/1有365天,但实际上它不到1年,如果使用PeriodType.years() ,则应截断为PeriodType.years()

In theory the same could happen for minutes because of leap seconds, but Joda doesn't support leap seconds. 从理论上讲,由于闰秒,同样可能会发生几分钟,但Joda不支持闰秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM