jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes compute duration in seconds Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "compute duration in seconds" Watch "compute duration in seconds" New topic

compute duration in seconds

John Davis
Ranch Hand

Joined: Nov 02, 2000
Posts: 189
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

Joined: Sep 28, 2004
Posts: 20538

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?


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Taariq San
Ranch Hand

Joined: Nov 20, 2007
Posts: 192
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.
I agree. Here's the link: http://aspose.com/file-tools
subject: compute duration in seconds
It's not a secret anymore!