• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Convert milliseconds to time

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way to convert milliseconds to a time value (hh:mm:ss)?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Will Paul
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Elapsed time in hh:mm:ss format
[ November 21, 2005: Message edited by: WillPaul ]
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First name space last name, please. Thanks for understanding.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, much appreciation!
[ November 23, 2005: Message edited by: Will Paul ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic