Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compute duration in seconds

 
John Davis
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following information:
IntervalStartDate: '2008-09-18'
IntervalStartTime: '17.48.33'
IntervalEndDate: '2008-09-19'
IntervalEndTime: '18.18.33'

I would like to compute duration of interval in seconds.

Can anyone suggest an easy way to do this (an open source object library?)?
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about concatenating the date and time strings, parse it to a date object, with the SimpleDateFormat class. Then take the difference of the two times from the date objects, and divide by 1000 to convert the interval from milliseconds to seconds?

Henry
 
Taariq San
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry's onto a winner there, but if you really must have an API just for this I suggest Joda Time.
Its very handy but for what you described its overkill and will take you longer to download, add to classpath, read the docs to understand how to use it to solve your particular problem and then write the code.

With Henry's solution you can skip the docs except to check input perhaps, Sun's javadocs also often have useful snippets of code so you can solve this one in a couple of minutes using standard API.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic