This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Adding 2 Times Together Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding 2 Times Together" Watch "Adding 2 Times Together" New topic
Author

Adding 2 Times Together

Steven Smith
Greenhorn

Joined: Dec 12, 2005
Posts: 14
How do I add 2 time stamps in hh:mm:ss format?
For example: 05:00:10 + 01:35:05
[ December 13, 2005: Message edited by: Steven Smith ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Hi Steven,

Welcome to JavaRanch!

Are these actual timestamp objects (e.g., instances of java.sql.Timestamp)? Or are these Strings representing a timestamp?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Steven Smith
Greenhorn

Joined: Dec 12, 2005
Posts: 14
Strings representing time stamps.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
You can parse time and date strings with java.text.SimpleDateFormat. What you want to do is add two time intervals, I think, since adding two times doesn't make that much sense (what is Oct 31 + Dec 25?) Some things to watch out for:

1. Set your date format's time zone to UTC, since you don't want to rassle with time zones.

2. I would extact the long time value from the parsed date and work with that. For instance you can add two of them together to add the two intervals. While you could turn around and use the date format to format the result as a string, like "05:00:10 + 01:35:05 = 06:35:15", watch out if the hours exceed 24 -- it will want to take you from 1 Jan 1970 to 2 Jan 1970! It's simple to format the time interval manually:


There is no emoticon for what I am feeling!
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

Originally posted by Steven Smith:
How do I add 2 time stamps in hh:mm:ss format?
For example: 05:00:10 + 01:35:05


Like Jeff said, adding two times doesn't mean a whole lot of sense.
Does the second timestamp represent a time interval, i.e, does the example expression mean 1 hr 35 mins and 5 seconds after 5:00:10 am/pm?
[ December 13, 2005: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions]
Steven Smith
Greenhorn

Joined: Dec 12, 2005
Posts: 14
Originally posted by Junilu Lacar:

Like Jeff said, adding two times doesn't mean a whole lot of sense.


Sorry, I just realized I shouldn't have wrote "time stamp." I think what I'm asking is much simpler. If driving my car from point A to point B takes 5 hours and 10 seconds (05:00:10) and driving from point B to point C takes 1 hour, 35 minutes and 5 seconds (01:35:05), I want to add the two times together (point A to C = 06:35:15) with everything in hh:mm:ss format. Also, if it took 25 hours to go from point A to point B, I would end up with 26:35:05, not a clock time. The two times would be string values already provided (in hh:mm:ss) but I need to add them together.
[ December 13, 2005: Message edited by: Steven Smith ]
Flo Powers
Ranch Hand

Joined: May 12, 2004
Posts: 57
Could you convert each time to seconds, add them together and then convert back to hours, minutes and seconds?

If you have 10.000 seconds, how many times does 3600 go into 10.000? The answer is 2, i.e. 2 hours. Use modulus to get the remaining seconds, i.e. 2800 seconds.

Next up is minutes: how many times does 60 go into 2800? The answer is 46, so you have 2 hours, 46 minutes. Next, use modulus to find the remainder, which is 40 seconds.

I think my thinking here is correct...

[ December 13, 2005: Message edited by: Flo Powers ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding 2 Times Together