• 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

Displaying seconds in hh:mm:ss format

 
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have created a countdown timer, and I want to display the time left in my GUI. I would like it to be displayed as hh:mm:ss. I can't think of how to do this.
Date works as miliseconds since 1970. The GregorianCalendar stores a full date. If I could insure days won't be used (I'm not sure I can, but lets suppose for a moment), then maybe I could create a GregorianCalendar instance and use military time and set the time left as the time since midnight. Still, this seems wrong.
Does anyone know an easy way to do this, or do I need to write a special parser to convert seconds into my desired String format?
--Mark
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I just wrote this up really quick (and didn't test it), but it seems to me to be along the lines of what you want. (Come to think of it, you will need to add leading zeros to the minutes and seconds to display "12:06:09" instead of "12:6:9".) This seems a little easier than fiddling with Dates and DateFormat objects, given the fact that you (theoreticaly) could have an hours count of greater than 24 (You could display it as "124:45:15" or, by extending this to include days, display it at "5d 4:45:15" or whatever)
[ July 02, 2003: Message edited by: Joel McNary ]
 
Mark Herschberg
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, that's exactly what I wanted to avoid, because I'll have to deal with leading zeros and other such formatting annoyances. the nice thing about Date and Calendar is that there's a simple way to parse them.
--Mark
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I'm missing something (this is the *advanced* forum after all) , but why not rely on some of the built-in time formatting?
Do you have a Date object already or are you working with the milliseconds? If you're working with milliseconds, does it bother you to create an intermediate Date object just to pass that to the formatter?

[ July 02, 2003: Message edited by: Michael Matola ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic