This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert time units (not Date conversion)" Watch "Convert time units (not Date conversion)" New topic
Author

Convert time units (not Date conversion)

Peter Fat
Greenhorn

Joined: Oct 18, 2010
Posts: 24
Hi!

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.

Thanks!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10175
    
    8

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
Greenhorn

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
Bartender

Joined: Oct 02, 2003
Posts: 11170
    
  16

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
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
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
Sheriff

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

Joined: Oct 27, 2005
Posts: 19656
    
  18

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Consider Paul's rocket mass heater.
 
subject: Convert time units (not Date conversion)
 
Similar Threads
Realistic Jump algorithm
What it takes to design a *good* software...
++ Operator
SCJP certificate
Passed SCEA Part 1 with 100%