aspose file tools*
The moose likes Java in General and the fly likes How to get Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to get "HH:mm:ss" from a long ?" Watch "How to get "HH:mm:ss" from a long ?" New topic
Author

How to get "HH:mm:ss" from a long ?

Francesco Marchioni
author
Ranch Hand

Joined: Sep 22, 2003
Posts: 191
Hi all,
I have a long value that holds how many seconds
the user is connected. I'd like to transalate in
the HH:mm:ss format. The problem is that the "HH"
fields starts with "01" instead of "00" !!

Let me explain better:
------------------------------------------------------------------
long time = 24000; // 24 seconds.

java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat("HH:mm:ss");

System.out.println("Time elapsed: " + sdf.format(new java.sql.Date(time)));
------------------------------------------------------------------
Instead of getting 00:00:24, I have 01:00:24 !
How can I get the hours starting from "00" ?
Thanks a lot
Francesco


WildFly 8 Administration Book - JBoss Tutorials
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Better go back and read the API again. That Date constructor wants time in milliseconds since the beginning of the UNIX era - Jan 1, 1970. It has nothing to do with elapsed time.
You would be better off doing the divisions and modulos to convert your elapsed time to hours, minutes and seconds and then formatting the string.
Bill
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
If you only want whole seconds it might be more painless to do the following:

You don't need to use SimpleDateFormat directly as the format you're using is not a special case.

The above approach is more "Java" than Bill's suggestion but almost certainly less efficient if performance is a factor.

Jules
Shashank Agarwal
Ranch Hand

Joined: May 20, 2004
Posts: 105
Try this,
let us suppose that the long object holding the no. of milliseconds is date1.

java.util.Date dateObject1 = new java.util.Date(date1);
System.out.println("Time is: " + dateObject1.getHours() + ":" + dateObject1.getMinutes() + ":" + dateObject1.getSeconds());
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get "HH:mm:ss" from a long ?