153

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?

3

4 Answers 4

481

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

7
  • Does the above take into account time zones? Or should I convert to UTC / GMT first?
    – Doo Dah
    Apr 21, 2016 at 1:15
  • @DooDah That's a good question, I "believe" that it does (as JodaTime has the concept of LocalTime), but I would suggest you test it and see. The question would be though, in which time zone would the resulting value be represented in? Apr 21, 2016 at 1:24
  • I will test and figure this out. For me, the resulting timezone will likely be the server the code is running on. I am implementing a scheduled queue and will need to compute the amount of time between now and some future time based on a date from another time zone. I will report back what I find.
    – Doo Dah
    Apr 21, 2016 at 1:29
  • 2
    I agree, this should have been the selected answer. Its neat and simple.
    – Sid
    Jan 9, 2018 at 12:12
  • Creating a new Duration object just to get a time difference just isn't cool. Imagine having to perform a time difference on a large array of date/time objects.
    – Johann
    Jun 16, 2018 at 7:20
84

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

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

long diffInMillis = d2.getMillis() - d1.getMillis();
3
  • 80
    The answer by MadProgrammer should have been the Accepted Answer. Notice the simple use of Minutes.minutesBetween. Feb 19, 2014 at 9:28
  • 5
    No I disagree, it's too wordy and unclear: "The minutes minutes between punch time date time" is worse than "second date milliseconds minus first date milliseconds"
    – Coder Guy
    Feb 19, 2015 at 23:11
  • 5
    This wil not take daylight savings into account. Sep 19, 2017 at 7:34
12

Something like...

Minutes.minutesBetween(getStart(), getEnd()).getMinutes();
1
  • Fails if the start and end date are really far apart (greater than size of int)
    – dannyRods
    Jan 9, 2020 at 17:55
6
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. 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().

In theory the same could happen for minutes because of leap seconds, but Joda doesn't support leap seconds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.