How to convert millisecond number to time format

How to convert a millisecond which is long to standard time format
[73 byte] By [uk0102a] at [2007-11-27 2:20:23]
# 1
new Date(long variable);gives you the date and you can play with it with SimpleDateFormat.
ayberka at 2007-7-12 2:21:39 > top of Java-index,Java Essentials,Java Programming...
# 2

[nobr]I have calculated difference between two times and got the result in millisecond i have done as u said

long dif=603

Date asdf=new Date(dif);

out.println(" <br>"+asdf);

now i am getting as

Thu Jan 01 05:30:00 IST 1970

what is this?[/nobr]

uk0102a at 2007-7-12 2:21:39 > top of Java-index,Java Essentials,Java Programming...
# 3

new Date(long variable);

adds the variable milliseconds to 1.1.1970 and gives you the new date as a Date object.

if you want to convert milliseconds to seconds or minutes you should make calculations .For ex;

double sec = milliseconds / 1000;

double minutes = milliseconds / 60000;

what do u need exactly?

ayberka at 2007-7-12 2:21:39 > top of Java-index,Java Essentials,Java Programming...
# 4

Jan 01, 00:00:00 IST 1970 is the magical start date for computers. I'm not sure why that date was picked specifically. I guess because it was a nice round date. But for all intents and purposes, we'll assume has to do with powerful magic.

You are passing in 603. What that does is adds 603 units of time to the start date.

What you are seeing is:

Thu Jan 01 00:00:00 IST 1970 + 603 = Thu Jan 01 05:30:00 IST 1970

You might want to look at this doc: http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Time.html

jjhusa01a at 2007-7-12 2:21:39 > top of Java-index,Java Essentials,Java Programming...
# 5

> > new Date(long variable);

>

>

> adds the variable milliseconds to 1.1.1970 and gives

> you the new date as a Date object.

>

> if you want to convert milliseconds to seconds or

> minutes you should make calculations .For ex;

>

> double sec = milliseconds / 1000;

> double minutes = milliseconds / 60000;

>

> what do u need exactly?

Actually I want to calculate the time difference between two given times so i have subtracted and got the value in milliseconds,I event got the conversion into hours,minutes,seconds,problem is after converting and when printing the result its coming as hh:mm:ss=4:12:9 but I want to print as 04:12:09 standard time format.So how to get the time like this.

uk0102a at 2007-7-12 2:21:39 > top of Java-index,Java Essentials,Java Programming...
# 6
You should be able to use SimpleDateFormat http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html to format your time.
jjhusa01a at 2007-7-12 2:21:40 > top of Java-index,Java Essentials,Java Programming...
# 7

I had to do the same thing just yesterday. I looked around a bit for a premade solution, then gave up & did it myself (wasnt that hard, but I wanted to avoid re-inventing something that might already be there):

long seconds = milliseconds / 1000;

String s = String.format("%1$02d:%2$02d:%3$02d", seconds / (60*60), (seconds / 60) % 60, seconds % 60);

I avoided the 'dateformat' classes because I wasnt sure how to tell it 'if I go over 24 hours, dont reset hour to 0 and increment day, instead show me 48 hours, 72 hours, etc'.

martinog2a at 2007-7-12 2:21:40 > top of Java-index,Java Essentials,Java Programming...
# 8
delete this post
martinog2a at 2007-7-12 2:21:40 > top of Java-index,Java Essentials,Java Programming...
# 9

> I had to do the same thing just yesterday. I looked

> around a bit for a premade solution, then gave up &

> did it myself (wasnt that hard, but I wanted to avoid

> re-inventing something that might already be there):

>

> long seconds = milliseconds / 1000;

> String s = String.format("%1$02d:%2$02d:%3$02d",

> seconds / (60*60), (seconds / 60) % 60, seconds %

> 60);

>

> I avoided the 'dateformat' classes because I wasnt

> sure how to tell it 'if I go over 24 hours, dont

> reset hour to 0 and increment day, instead show me 48

> hours, 72 hours, etc'.

Thanks martinog .Anyhow even i got the solution but i will even try urs

uk0102a at 2007-7-12 2:21:40 > top of Java-index,Java Essentials,Java Programming...
# 10

Using the calender should ideally solve the purpose of getting the present time, in the best way

Calendar cal = Calendar.getInstance(TimeZone.getDefault());

String DATE_FORMAT = "yyMMddHHmmss";

java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);

String name = ""+sdf.format(cal.getTime());

This code works fine for me, hope it is of help to u 2.

cheerz!!

sun_java_helpa at 2007-7-12 2:21:40 > top of Java-index,Java Essentials,Java Programming...