• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adding 2 Times Together

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Steven,

Welcome to JavaRanch!

Are these actual timestamp objects (e.g., instances of java.sql.Timestamp)? Or are these Strings representing a timestamp?
 
Steven Smith
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings representing time stamps.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Steven Smith
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic