• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting time and difference between two times  RSS feed

 
Ashish S Yadav
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all ! I am new here Please help me with the problem below:

I want to get only the time (hours, minutes,seconds) using java. Then i want to subtract that time from another time to get the time difference.
How can I do this ?

I get the time as a string modifying option 2 at a link given below. But i wanted the time in a form where i can simply subtract one time from another, just like numbers.
http://www.mkyong.com/java/java-how-to-get-current-date-time-date-and-calender/



 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ashish, welcome to CodeRanch.

The matter really depends on what format your two times are in. If they are simple Unix timestamps (like the values you retrieve from System.currentTimeMillis()) then you can simply subtract one value from the other, and the result will be the difference in milliseconds, either positive or negative.

If you get your times as Strings, you should first parse them to Date objects using a DateFormat, and then you can get their respective Unix timestamps using the Date.getTime() method.
 
Ashish S Yadav
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any sample code for this ?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A sample should be pretty trivial after reading what I wrote above. Why don't you take a look at the System.currentTimeMillis() method and experiment with it a little bit?

For instance, try waiting for a while between two consecutive calls and see what happens when you print the difference between the two timestamps.
 
Ashish S Yadav
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nm. Did it using another method. Thanks anyway.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!