• 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

format time, ignoring locale

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a "long" variable with the milliseconds a process took to complete.
How can I print this in a hh:mm:ss format?

I tried



but it's incorrect, because it's locale specific. Can I make it ignore the locale?

Is there a better way of doing this?

Thanks,
lajos
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you want the output to look like?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's time-zone specific. If you want to ignore where you are in the world, what time zone would you like to use? You can do something like:

to set the time zone to, um, GMT - 6 hours. Or use TimeZone.getAvailableIDs() to get a list of timezones to choose from. These will obey the daylight saving rules currently in place for whatever name/location you choose. Up until one of the countries changes the rules once again. :roll: If you use GMT you don't need to worry about that; you can just confuse everyone equally.
 
lajos kamocsay
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to print the elapsed time as:

hh:mm:ss

Maybe I just need to do some math and not try relying on the API?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that would probably be simpler, if you're talking about an elapsed time. The Date and DateFormat classes have built-in assumptions that you're talking about an absolute time (which, to be displayed, needs a time zone). Whereas a difference in time has no time zone. You could probably fake this by setting the time zone to GMT, but it's a bit confusing. Note also that using SimpleDateFormat, 'hh' will give you values in the range 1-12 - it will say '12' rather than '00'. You would probably want 'HH' instead. And you might also want to add days (maybe with 'DDD') if there's a chance the elapsed time might be greater than 24 hours. In comparison, doing a little math to calculate minutes, hours, and days yourself is probably simpler.
 
lajos kamocsay
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I did the math. Easier that way.

BTW: I cannot believe Calendar.JANUARY=0. Whoever did that, should be denied pancakes for the rest of his life.
reply
    Bookmark Topic Watch Topic
  • New Topic