This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying seconds in hh:mm:ss format

 
Mark Herschberg
Sheriff
Posts: 6037
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Joel McNary
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 6037
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1797
4
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic