wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Time Difference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Time Difference

R Sugan
Ranch Hand

Joined: Mar 13, 2009
Posts: 48
Hi,

I need to check the difference between the time in database and system time.I am having both the times in yy:mm:dd hh:mm:ss format.please reply me how to do this .very urgent.I am getting both the time in string like

String calledtime=rs.getString("calledtime");
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss") ;
String currenttime = formatter.format (new Date()) ;




I am getting the output printed as
2008-05-02 16:52:17
2008-05-20 15:10:44


I need this output like this 00:00:00

00:00:01
00:00:02
00:00:03
00:00:04
00:00:05


Thanks.
Aurelian Tutuianu
Ranch Hand

Joined: May 13, 2004
Posts: 86
You have to obtain the to dates in Date instances. From there you get two milliseconds using Date.getTime. After that obtain the difference in milliseconds and with the difference create a new Date. Using SimpleDateFormat you cand print the difference.

So, as a rule of thumb, obtain milliseconds and do calculations in milliseconds. Don't parse or use Strings, is too complicated.

An example:

Notice the usage of TimeZone, that is important.

http://javasign.blogspot.com/
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Closing as duplicate of this post. Please read this FAQ.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Time Difference
 
Similar Threads
Time format in Java
Total Time duration
Time Difference
Time Zone problem
time difference