• 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

function to convent the second to duration format

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Steve Jiang
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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




 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Steve Jiang
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic