Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert the seconds in this format "HH:mm:ss"

Tags:

java

android

time

I want to convert the second/milliseconds in this format "HH:mm:ss" (for esamples, from 5 seconds to 00:00:05). I tried to get that format in this way:

int millis = 5000; SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); String time = df.format(millis); 

In that way, I get "01:00:05" and not "00:00:05". Where am I wrong?

like image 493
Webman Avatar asked Feb 09 '12 16:02

Webman


People also ask

How do you convert HH mm SS to seconds?

To convert hh:mm:ss to seconds:Convert the hours to seconds, by multiplying by 60 twice. Convert the minutes to seconds by multiplying by 60 .

How do you convert seconds to HH mm SS format in Excel?

As with Excel, the first step to converting elapsed second to time is to divide the value by 86400. To format the cells for mm:ss, select Format > Number > More Formats > More date and time formats from the Menu.

How do you convert seconds to HH mm SS format in SQL?

SELECT SEC_TO_TIME( seconds ); SELECT SEC_TO_TIME( seconds ); In this query, the “seconds” parameter is the number of seconds to convert. The result will be displayed in “HH:MM:SS” format.


1 Answers

Timezones.

The long value 5000 means 5 seconds after the epoch. For the majority of timezones, 5 seconds after the epoch is not 5 seconds past midnight local time.

Java 8 update:

java.time.LocalTime will handle the idea of a wall-clock "time of day" without you having to worry about the timezones and days implicit in java.util.Date. If you can use Java 8, and your durations will always be less than a day, then a correct version of your example can be as simple as:

int millis = 5000; int seconds = millis / 1000; // Maybe no need to divide if the input is in seconds LocalTime timeOfDay = LocalTime.ofSecondOfDay(seconds); String time = timeOfDay.toString(); 

(I guess strictly speaking, java.time.Duration is a better model of what you want, in that it represents a certain number of seconds, rather than a time-of-day. But it's a pain to format into hh:mm:ss, so if you're always dealing with sub-24hour values, TimeOfDay gives you this formatting for free and is otherwise equivalent.)


If you're stuck with Java 7 or below, then explicitly specifying a timezone of GMT in your example code should give you the output you expect.

Here's a Scala REPL session demonstrating the problem, and Java 7 solution, on my machine:

scala> val millis = 5000 millis: Int = 5000  scala> val df = new java.text.SimpleDateFormat("HH:mm:ss") df: java.text.SimpleDateFormat = java.text.SimpleDateFormat@8140d380  scala> df.format(millis) res0: java.lang.String = 01:00:05  scala> df.getTimeZone.getID res1: java.lang.String = GB  scala> df.getTimeZone.getOffset(millis) res2: Int = 3600000  scala> df.setTimeZone(java.util.TimeZone.getTimeZone("GMT"))  scala> df.format(millis) res3: java.lang.String = 00:00:05 

So you can see that my default time zone is GB, which has a 1 hour offset from GMT at the time denoted by 5000L. Setting the timezone to GMT gievs the expected output of 00:00:05.

like image 177
Andrzej Doyle Avatar answered Oct 08 '22 11:10

Andrzej Doyle