jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Converting to Milliseconds Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Converting to Milliseconds" Watch "Converting to Milliseconds" New topic

Converting to Milliseconds

Shriram Gopalakrishnan
Ranch Hand

Joined: Feb 20, 2007
Posts: 44
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

Joined: Dec 28, 2006
Posts: 893
You can solve this my using the java.text.SimpleDateFormat class

I hope this helps

Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Shriram Gopalakrishnan
Ranch Hand

Joined: Feb 20, 2007
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 Please suggest me if there is other way to do so.
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Define 'wrong'.
Sanity check:

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

Which looks more likely?
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
You may find this recent discussion to be useful.

"I'm not back." - Bill Harding, Twister
Shriram Gopalakrishnan
Ranch Hand

Joined: Feb 20, 2007
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.
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
java.util.Date (and Calendar) is designed to store absolute time (and date), not time intervals. It stores time in UTC (and the millisecond value is based on UTC) but displays it in local time.

Correct result for 2935000ms is 48:55 but when used with java.util.Date, the value means 1.1.1790 00:48:55 UTC which seems to be 06:18:55 in your local time zone (+5 and half hour).
I agree. Here's the link: http://aspose.com/file-tools
subject: Converting to Milliseconds
jQuery in Action, 3rd edition