Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# Converting to Milliseconds

Shriram Gopalakrishnan
Ranch Hand
Posts: 44
Hi,
Can anyone help me to convert milliseconds to hh.mm.ss format. Tried the code below but ended up with in-accurate results.

public static void convertFromMillisecondsToString() {
int s = 2935000;
Date d = new Date(s);
System.out.println("time in millisecs: "+s);
System.out.println("time: "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());
}

Remko Strating
Ranch Hand
Posts: 893
You can solve this my using the java.text.SimpleDateFormat class

I hope this helps

Shriram Gopalakrishnan
Ranch Hand
Posts: 44
I tried even that and it gives me 06.18.55 which is wrong, actually if you convert 2935000 to hh.mm.ss it should give 00.48.91.66. Please suggest me if there is other way to do so.

David O'Meara
Rancher
Posts: 13459
Define 'wrong'.
Sanity check:

How many milliseconds is 6 hours?
How many milliseconds is 48 hours?

Which looks more likely?

Jim Yingst
Wanderer
Sheriff
Posts: 18671
You may find this recent discussion to be useful.

Shriram Gopalakrishnan
Ranch Hand
Posts: 44
6 hrs =21600 s
18 mins = 1080 s
55 s
--------
22735 s * 1000 = 22735000 millisecs

22735000 is not equal to 2935000 which is the input.

So output time in h:m:s is not equal to input millisecs.