File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Convert milliseconds to time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert milliseconds to time" Watch "Convert milliseconds to time" New topic
Author

Convert milliseconds to time

Will Paul
Greenhorn

Joined: May 27, 2005
Posts: 8
Is there a way to convert milliseconds to a time value (hh:mm:ss)?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Initials aren't enough for a last name. You can change your display name here. Thanks!

As for your question: are you talking about elapsed time, or absolute (calendar) time?


[Jess in Action][AskingGoodQuestions]
Will Paul
Greenhorn

Joined: May 27, 2005
Posts: 8
Elapsed time in hh:mm:ss format
[ November 21, 2005: Message edited by: WillPaul ]
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Yes, division.



There's probably more efficient ways of doing the math. I can think of at least three or four ways off the top of my head but I have no idea which is most efficient or if it even matters.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

First name space last name, please. Thanks for understanding.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
You can construct a java.util.Date from the time interval in milliseconds and format it using SimpleDateFormat.

Take care that your results are not messed-up by issues such as time zone. When I've done this, I've defined a special "null" time zone, which has no offset or daylight saving.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Hmmm, using a Date for this strikes me as inappropriate. The class is designed to represent an absolute time, not a relative time. Invoking toString() is sufficient to see that this is the case. I suppose it could be OK if the Date instance is only used internal to a method, and never passed on to any other class where the programmer may make the "mistake" of treating the Date as, well, a Date. If you want to abuse Date instances, make sure you do it in the privace and safety of your own home.

[Peter Chase]: When I've done this, I've defined a special "null" time zone, which has no offset or daylight saving.

Like, say, GMT?

Will: you may also want to consider whether these time values can ever exceed 24 hours. And if so, how do you want them displayed? SimpleDateFormat can't really handle this elegantly. You're probably better off doing the math yourself, as Ken showed.


"I'm not back." - Bill Harding, Twister
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Yeah, obviously any logic regarding the maximum number of hours would be needed if desired. It also makes no guarantee a time greater than 99 hours is left at 99 to maintain an HH format. I wouldn't use a date simply because it's not a date. It's an amount of time that has elapsed, which has nothing to do with anything related to calendar time or time zones.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Think about making a little elapsed time class. This could be a fun OO exercise. Could you make these print true?

How about another constructor with before & after times in milliseconds? Should you have days() and weeks() methods? (I'll vote NO for months() or anything bigger.) What would a good toString() implementation say?
[ November 22, 2005: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Stan James:
Think about making a little elapsed time class. This could be a fun OO exercise. Could you make these print true?

How about another constructor with before & after times in milliseconds? Should you have days() and weeks() methods? (I'll vote NO for months() or anything bigger.) What would a good toString() implementation say?

[ November 22, 2005: Message edited by: Stan James ]


That might be overkill for his situation. If I were going to write something to do this I think I'd create a class that can be instantiated with milliseconds and then could return milliseconds, seconds, minutes, hours. You could then create a formatter capable of converting that to a specific format such as HH:MM:SS, HHHH:MM:SS. I suppose you could add in support for days, weeks and months too.
Will Paul
Greenhorn

Joined: May 27, 2005
Posts: 8
Thanks, much appreciation!
[ November 23, 2005: Message edited by: Will Paul ]
Clare
Greenhorn

Joined: May 11, 2004
Posts: 1
import java.text.SimpleDateFormat;
import java.util.Date;

public class Convert {
public String getDateTime(String pattern){
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(new Date());
}
/**
* @param args
*/
public static void main(String[] args) {
Convert con = new Convert();
System.out.println(con.getDateTime("hh:mm:ss"));
}
}
[ November 23, 2005: Message edited by: csie clare ]
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by csie clare:
import java.text.SimpleDateFormat;
import java.util.Date;

public class Convert {
public String getDateTime(String pattern){
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(new Date());
}
/**
* @param args
*/
public static void main(String[] args) {
Convert con = new Convert();
System.out.println(con.getDateTime("hh:mm:ss"));
}
}

[ November 23, 2005: Message edited by: csie clare ]


Problem with that is it treats it as a date. For example, running that with a time of 5000 gives me 04:00:05 rather than 00:00:05. If I ran it in two hours it'd give me 06:00:05 instead of 00:00:05. Others will get different results too depending on their time zone, etc.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If I were going to write something to do this I think I'd create a class that can be instantiated with milliseconds and then could return milliseconds, seconds, minutes, hours. You could then create a formatter capable of converting that to a specific format such as HH:MM:SS, HHHH:MM:SS. I suppose you could add in support for days, weeks and months too.


Ken, I think we said the same thing, no? You added a separate formatter that would be a good separation of concerns. Anything beyond the current requirements would be "overkill" ... or at least not The Simplest Thing That Can Possibly Work (right now) because chances are Ya Ain't Gonna Need It anyhow. I might hold off on the formatter until I needed the second format.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Then I misunderstood. You mentioned a toString() implementation but I didn't see any mentiong of setting up any kind of formatter. Or maybe it was intended that the class itself would be created with a format specified and toString() would return the formatted result. Either way, I was merely trying to extend what you were saying a level further and suggest a class that converts milliseconds into seconds, hours, etc. and a formatter interface that uses it to provide a specified format.

I might create it purely as an intellectual exercise, probably not.
Tom Wheeler
Greenhorn

Joined: Apr 23, 2004
Posts: 8
Thanks for the help here. I used what I had and what was posted here and I came up with this.

Worked well for me.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert milliseconds to time
 
Similar Threads
averaging time component of a calendar object
Converting millisecs to hour, minute, seconds.
Converting Date Into Milliseconds?
DATE/TIME
Convert hh:mm:ss in millisecond