File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes  function to convent the  second to duration format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark " function to convent the  second to duration format" Watch " function to convent the  second to duration format" New topic
Author

function to convent the second to duration format

Steve Jiang
Ranch Hand

Joined: May 17, 2004
Posts: 106
Hi,

Is there general function to be used to convert the second to duration format, like 3680 seconds convert as 1 hour 1 min 20 second. Ideally it could be based on pattern, like 1 week 2 day , or 2 hour 30 min (based on the second value to decide the format to fit the UI width , for example, if data is too long like 1 year 2 month 3 day 4 hour 5 min 6 sec, it only show 1 year 2 month, but for the 63 second just 1 min 3 sec), or some similar function.

The function may not be very complex, and could be write by own. But I am wondering if there is existing function which could be used directly.

Thanks


Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

There's no built-in method for that in the standard Java API.

If you would use Joda Time, which is a popular library, much better than the standard library for working with dates and times, you could do this:

Note that this might be more complex than you think. For example, how long is 1 month? It's not a fixed number of days. Likewise, a year is not always equally long, and even a day is not always equally long (with regard to daylight savings, days are sometimes 23 or 25 hours instead of 24 hours).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Steve Jiang
Ranch Hand

Joined: May 17, 2004
Posts: 106
Thanks a lot for your suggestions!

when I use the following code :




and the following Junit test code




All other test passed, except the last one failed as

org.junit.ComparisonFailure: expected:<'[1 year 1 month 1 day 1 hour] 1 minute 1 second'> but was:<'[9385 hours] 1 minute 1 second'>


I don't know why PeriodFormatterBuilde doesn't convert the hours to day , month and year again.

Do you have any suggestions for the issue?

Thanks




Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

Just a thought - could this be caused by last year being a leap year? Because one year ago was March 23rd 2012, and 30 days before that was not Februari 23rd 2012 (one month) but February 22nd 2012.
Although, if it wasn't a leap year 30 days before would be February 21st, not 23rd.

So my advice - try using 30 days instead of 1 month in the comparison. And hope you don't encounter DST.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Steve Jiang
Ranch Hand

Joined: May 17, 2004
Posts: 106
Rob Spoor wrote:Just a thought - could this be caused by last year being a leap year? Because one year ago was March 23rd 2012, and 30 days before that was not Februari 23rd 2012 (one month) but February 22nd 2012.
Although, if it wasn't a leap year 30 days before would be February 21st, not 23rd.

So my advice - try using 30 days instead of 1 month in the comparison. And hope you don't encounter DST.


I just tested


and the code is still return

org.junit.ComparisonFailure: expected:<'[2 day 12 hours] 2 minutes 15 second...> but was:<'[60 hours] 2 minutes 15 second...>


maybe missed some configuration? but I didn't find it.

Steve Jiang
Ranch Hand

Joined: May 17, 2004
Posts: 106
I added the PeriodType.yearDayTime() in the period constructor, but still get 26 hours, instead of 1 day 2 hours.


Any more suggestions for this issue?

thank you!





add completed junit test code

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: function to convent the second to duration format
 
Similar Threads
Convert String to Date
having problem with a stored procedure used called by a DAO
Java - Date conversion to milliseconds problem
Convert date time to file date time
New Year, New Forum Software