[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]
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?
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
> > 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.
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'.
> 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
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!!