splitting seconds into Hr, Min and Sec, and formating number

R

Robocop

Hi folks, i wanna split seconds into hours, minutes and seconds

if I have: 35800 seconds
I want to have output as follows

Orbital Period = 23 hours, 56 minutes, and 29.0 seconds

*Formating number*

if i get 34.87234524 how to formate it to get 34.87 in output only
upto two decimal places! , I tried using Math.round etc doesn't work!

Thanks very much for help!!! :)
 
P

P.Hill

Robocop said:
Hi folks, i wanna split seconds into hours, minutes and seconds
*Formating number*

Yup you are on the right track try java.text.SimpleDateFormat
push **MILLI** seconds into a java.util.Date use an appropriate for
DateFormat mySimpleFormat = new SimpleDateFormat( "somethign with h's, m's and
s' and maybe some colons" );
System.out.println( mySimpleDateFormat.format( new Date( mySeconds * 1000 ) ) );

Close enough?

Try the javaDocs.

-Paul
 
L

Lee Fesperman

Robocop said:
*Formating number*

if i get 34.87234524 how to formate it to get 34.87 in output only
upto two decimal places! , I tried using Math.round etc doesn't work!

Use java.text.DecimalFormat.
 
S

Sean Berry

This may not be what you are looking for at all... but may help.

Cast your seconds to be an int.
Then
int hours = seconds/3600
// equals 9
int tempminutes = seconds%3600
// equals 3400
int minutes = tempminutes/60
// equlas 56
int seconds = tempminutes%60
// eqals 40
so you get 35800 seconds = 9 hours, 56 minutes, 40 seconds.
Format it in any way you like
Orbital Period = 23 hours, 56 minutes, and 29.0 seconds

This corresponds to 86189 seconds... not 35800.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top