File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Convert time units (not Date conversion) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert time units (not Date conversion)" Watch "Convert time units (not Date conversion)" New topic

Convert time units (not Date conversion)

Peter Fat

Joined: Oct 18, 2010
Posts: 24

I have a time interval of, for example, 90632 secons, and I want to present this in the format: 1 day 1 hour 10 minutes and 32 seconds.

Is there anything that can help me with this? I prefer to use a already existing framework, than to write the code from scratch for this.

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10767

A day has 24 hours.
An hour has 60 minutes.
A minute has 60 seconds.
Should be straight forward calculation.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Peter Fat

Joined: Oct 18, 2010
Posts: 24
Seriously? A day has 24 hours?! Wow...

Obviously I know how to convert time. But I prefer to reuse already existing code who does it. I'm gonna deduce from your answer, that you know none. Thanks anyway.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11917

did you try searching? The java API has a class that may help you with time units...

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45277
We probably don’t use that sort of class this time of year. In November we get the tyros, who have to do that sort of thing with arithmetic. Of course you can remind yourself of the arithmetical operators, and work out which can convert seconds to minutes and seconds. Once you have worked that out you repeat the procedure for hours ...
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45277
Actually, it is nearly as fast to write code from scratch for something this simple.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20181

I'm with Fred on this one. The java.util.concurrent package has an enum that's designed for just these kinds of things.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Convert time units (not Date conversion)
It's not a secret anymore!