Calculating date/time difference in Java

+1 vote

I have a code which calculates the difference between two dates in terms of hours/minutes/seconds. But when I am executing the below code, it is not giving me the accurate difference in seconds. Can someone suggest, where my code is going wrong?

String startDate = "11/03/14 09:29:58";
String stopDate = "11/03/14 09:33:43";

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

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

// Get msec from each, and subtract.
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000;         
long diffMinutes = diff / (60 * 1000);         
long diffHours = diff / (60 * 60 * 1000);                      
System.out.println("Time in seconds: " + diffSeconds + " seconds.");         
System.out.println("Time in minutes: " + diffMinutes + " minutes.");         
System.out.println("Time in hours: " + diffHours + " hours.");

My output is:

Time in seconds: 225 seconds.
Time in minutes: 3 minutes.
Time in hours: 0 hours.

But the correct output should be:

Time in seconds: 45 seconds.
Time in minutes: 3 minutes.
Time in hours: 0 hours.

Please help.

Jun 19, 2018 in Java by v.liyyah
• 1,300 points
520 views

2 answers to this question.

0 votes

Hi… you can try using the below code to fix your program:

long diffSeconds = diff / 1000 % 60;  
long diffMinutes = diff / (60 * 1000) % 60;
answered Jun 19, 2018 by geek.erkami
• 2,320 points
0 votes
We can also use java.util.concurrent.TimeUnit class.
long diff = d2.getTime() - d1.getTime();//as given

long seconds = TimeUnit.MILLISECONDS.toSeconds(diff);
long minutes = TimeUnit.MILLISECONDS.toMinutes(diff); 
answered Aug 28, 2018 by Sushmita
• 6,880 points

Related Questions In Java

0 votes
1 answer

How to calculate the difference between two date instances in Java?

You can use Joda Time Library. Interval i ...READ MORE

answered May 4, 2018 in Java by parth295
• 4,640 points
58 views
0 votes
2 answers

How do I get the current date and time using Java?

If you require a time stamp in ...READ MORE

answered Aug 23 in Java by Sirajul
• 29,740 points
93 views
0 votes
2 answers

How can I get current time in YYYY:MM:DD HH:MI:Sec:Millisecond format in Java?

public String getCurrentTimeStamp() { ...READ MORE

answered Sep 21, 2018 in Java by parth295
• 4,640 points
539 views
0 votes
1 answer

difference between wait() and sleep() in java

We can find a big difference between ...READ MORE

answered Apr 25, 2018 in Java by developer_1
• 3,300 points
67 views
0 votes
1 answer

How can I get the current date and time in UTC or GMT in Java?

This definitely returns UTC time: as String ...READ MORE

answered Jun 7, 2018 in Java by Rishabh
• 3,540 points
7,082 views
0 votes
1 answer

Measure time elapsed in Java?

This is also a way to work ...READ MORE

answered Jul 3, 2018 in Java by scarlett
• 1,280 points
45 views
0 votes
1 answer

Convert Milliseconds to “X mins, x seconds” in Java?

I wrote a function which converts milliseconds ...READ MORE

answered May 30, 2018 in Java by Rishabh
• 3,540 points
758 views
0 votes
2 answers

Date format conversion in Java

Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, 1); Date date = ...READ MORE

answered Aug 13, 2018 in Java by Sushmita
• 6,880 points
59 views
0 votes
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,320 points
465 views
0 votes
1 answer

Sorting an ArrayList in Java

You can easily do this by simply ...READ MORE

answered May 4, 2018 in Java by geek.erkami
• 2,320 points
213 views