Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding value to A Time

 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a String of the form "03:20:10,132" representing a time value. What is the simpliest way to add an arbitrary time value to it? I think it isn't a good choice to manually parse the string to identify which is hour, minute and second. Is there a faster method?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to play with the Calendar class.
 
mohit bahl
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
One way of solving your problem is that you can use the SimpleDateFormat calss to convert this String to a Date object. Then using this date object you can build/set a GregorianCalendar Object. And using the add function of this GregorianCalendar class you can add any number to any field, like hour minute or seconds.
Just remember one thing, when you create date object by providing only the time details, date is defaulted to Jan 01 1970.

Hope this gives you some direction to work on.

cheers!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic