• Post Reply Bookmark Topic Watch Topic
  • New Topic

format time, ignoring locale  RSS feed

 
lajos kamocsay
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • 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
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you want the output to look like?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!