0

I am trying to compute the difference in time between two date/times. This is what I have imported:

import java.util.concurrent.TimeUnit;
import java.text.*;
import java.util.*;

And this is my code:

SimpleDateFormat format= new SimpleDateFormat("yy/MM/dd HH:mm:ss");


String dateStart = "11/03/14 19:29:58";
String dateStop = "11/05/14 08:03:13";

Date d1=null;
Date d2=null;
try{
    d1 = format.parse(dateStart);
    d2=format.parse(dateStop);
}
catch(ParseException e)
{
    e.printStackTrace();
}

long diff = d2.getTime()-d1.getTime();

long seconds = TimeUnit.MILLISECONDS.toSeconds(diff);
long minutes=TimeUnit.MILLISECONDS.toMinutes(diff);
System.out.println(minutes);

This, I think, should work fine, but instead of the correct answer I get "87153". What is the source of error and how can I correct it?

Raj Raina
  • 211
  • 1
  • 11

3 Answers3

6

That looks correct to me... you've got just under two months, so 60.5 days (because it starts at ~8pm and ends at ~8am). A day contains 1440 minutes. 1440 * 60.5 is 87120, which is very close to the answer you've received.

It's not clear what answer you expected, but Java's getting the maths right. The source of the error is presumably your expectations :)

(As an aside, you may well want to specify a time zone of UTC unless you want calculations like this to use your local time zone. You could easily get answers which are an hour off your expected results for that reason.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

This seems to be correct.

long seconds = TimeUnit.MILLISECONDS.toSeconds(diff);
System.out.println(seconds);
long minutes=TimeUnit.MILLISECONDS.toMinutes(diff);
System.out.println(minutes);
long hours=TimeUnit.MILLISECONDS.toHours(diff);
System.out.println(hours);
long days=TimeUnit.MILLISECONDS.toDays(diff);
System.out.println(days);

Gives output as

5229195
87153
1452
60

This seems to be correct... You have difference of two months right?

Or else check with your assumption of dates. May be you are making some false assumption with dd and yy, try to put yyyy and inside your year also correct it to 2011 or 2014 as per your expectation. This is my assuption :P

user3657302
  • 347
  • 1
  • 5
  • Yes, this is exactly what happened. I thought I was inputting Month/Date/Year, as is standard in the United States. – Raj Raina Jul 07 '14 at 21:58
0

Always use calendars:

import java.text.SimpleDateFormat;
import java.util.concurrent.TimeUnit;
import java.text.*;
import java.util.*;

public class DateDiff {
    public static void main(String[] arg){
        SimpleDateFormat format= new SimpleDateFormat("yy/MM/dd HH:mm:ss");


        String dateStart = "11/03/14 08:03:58";
        String dateStop = "11/05/14 08:20:13";

        Date d1=null;
        Date d2=null;
        try{
            d1 = format.parse(dateStart);
            d2 = format.parse(dateStop);
        }
        catch(ParseException e)
        {
            e.printStackTrace();
        }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(d1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(d2);
        long seconds = cal2.get(Calendar.SECOND) - cal1.get(Calendar.SECOND);
        long minutes= cal2.get(Calendar.MINUTE) - cal1.get(Calendar.MINUTE);
        System.out.println(minutes);
        System.out.println(seconds);
    }
}
Mansueli
  • 6,223
  • 8
  • 33
  • 57