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,290 points
101 views

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,820 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,590 points
36 views
0 votes
1 answer

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

To print current date and time using ...READ MORE

answered Apr 13, 2018 in Java by Akrati
• 3,150 points
39 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,590 points
71 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,220 points
44 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,520 points
3,286 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
32 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,520 points
423 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,820 points
39 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
204 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
138 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.