Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting to Milliseconds

 
Shriram Gopalakrishnan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can solve this my using the java.text.SimpleDateFormat class



I hope this helps
 
Shriram Gopalakrishnan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may find this recent discussion to be useful.
 
Shriram Gopalakrishnan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic