Depending on your locale, it looks like it was the ending of Daylight Savings Time a.k.a Summer Time. Here's the 2009 table; October 25, 2009 was the ending date for many locales. It would explain why 66 showed up instead of 6; there were 60 extra minutes.
Wrong time calculated in minutes from two Date objects to calculate duration
Question
I am trying to calculate the duration between two date objects in minutes.
I have found some inspiration during my research from this stackoverflow question. In generally this seems to work correct, but I'm experiencing an interesting behaviour for one testcase.
When I run the below attached sourcecode (You can simply copy past it), it returns me 66 minutes instead of (the correct result) 6 and I currently don't understand why. Perhaps I'm overseeing something at the moment, can you tell me what it is?
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class Test {
private SimpleDateFormat parserSDF = new SimpleDateFormat("MM/dd/yy HH:mm",
Locale.ENGLISH);
public static void main(String[] args) {
Test test = new Test();
Date begin = test.createDateFromString("10/25/09 1:54");
Date end = test.createDateFromString("10/25/09 2:00");
int duration = test.minutesDiff(begin, end);
//result is 66
System.out.println(duration);
}
public int minutesDiff(Date earlierDate, Date laterDate) {
if (earlierDate == null || laterDate == null)
return 0;
return (int) ((laterDate.getTime() / 60000) - (earlierDate.getTime() / 60000));
}
public Date createDateFromString(String dateString) {
Date date = null;
try {
date = parserSDF.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
I know btw that there exists this Joda library which does way better in computing this stuff, but if possible, I would like to stay without an external library.
Thanks for every thought you're sharing with me.
EDIT: Aaaah, might it be possible that this is because clock change? Clock change was at the 25th of October 2009 and the time got set back from 3am to 2am. This could mean that this result is correct
Solution
OTHER TIPS
The date-time API of java.util
and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API.
- For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7.
- If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
Demo:
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("M/d/uu H:m", Locale.ENGLISH);
ZoneId zoneId = ZoneId.of("America/New_York");// Change it the required timezone
ZonedDateTime begin = LocalDateTime.parse("10/25/09 1:54", dtf).atZone(zoneId);
ZonedDateTime end = LocalDateTime.parse("10/25/09 2:00", dtf).atZone(zoneId);
long minutes = Duration.between(begin, end).toMinutes();
System.out.println(minutes);
}
}
Output:
6
Learn about the modern date-time API from Trail: Date Time.
laterDate.getTime()
return the long
value in mili-seconds. You are casting long value to int. That will cause to incorrect time deference.