How to Convert Milliseconds to "Hh:Mm:Ss" Format

How to convert milliseconds to hh:mm:ss format?

You were really close:

String.format("%02d:%02d:%02d", 
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), // The change is in this line
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));

You were converting hours to millisseconds using minutes instead of hours.

BTW, I like your use of the TimeUnit API :)

Here's some test code:

public static void main(String[] args) throws ParseException {
long millis = 3600000;
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(hms);
}

Output:

01:00:00

I realised that my code above can be greatly simplified by using a modulus division instead of subtraction:

String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) % TimeUnit.HOURS.toMinutes(1),
TimeUnit.MILLISECONDS.toSeconds(millis) % TimeUnit.MINUTES.toSeconds(1));

Still using the TimeUnit API for all magic values, and gives exactly the same output.

How to convert milliseconds to mm:ss:ms format?

From System.currentTimeMillis():

Returns the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

So what you see in your output is the Hh:mm:ss portion of the time taken between 1970-01-01T00:00:00 up until now.

What you actually want is the taken time i.e the difference between start and end of your time measurement.

val start = System.currentTimeMillis()

// do your processing

val end = System.currentTimeMillis()
val time = String.format("%1$tM min. %1$tS sec. %1$tL ms.", end - start)

This should give you an appropriate output.


As noted in the comments weird stuff happens in some timezones. And String.format() seems to be quite unconfigurable (at least I didn't find anything).

If you really want to be on the safe side, you can use the answer suggested by @SergeyAfinogenov, but with some minor tweaks:

val minutes = duration.getSeconds() / 60
val seconds = duration.getSeconds() - minutes * 60
val millis = duration.getNano() / 1_000_000
val time = String.format("%d min. %d sec. %d ms.%n", minutes, seconds, millis)

This effectively manually calculates the different parts (minutes, seconds, millis) from the Duration and then formats them accordingly.

How can I convert milliseconds to hhmmss format using javascript?

const secDiff = timeDiff / 1000; //in s
const minDiff = timeDiff / 60 / 1000; //in minutes
const hDiff = timeDiff / 3600 / 1000; //in hours

updated

function msToHMS( ms ) {
// 1- Convert to seconds:
let seconds = ms / 1000;
// 2- Extract hours:
const hours = parseInt( seconds / 3600 ); // 3,600 seconds in 1 hour
seconds = seconds % 3600; // seconds remaining after extracting hours
// 3- Extract minutes:
const minutes = parseInt( seconds / 60 ); // 60 seconds in 1 minute
// 4- Keep only seconds not extracted to minutes:
seconds = seconds % 60;
alert( hours+":"+minutes+":"+seconds);
}

const timespan = 2568370873;
msToHMS( timespan );

Demo

How to convert Milliseconds to X mins, x seconds in Java?

Use the java.util.concurrent.TimeUnit class:

String.format("%d min, %d sec", 
TimeUnit.MILLISECONDS.toMinutes(millis),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
);

Note: TimeUnit is part of the Java 1.5 specification, but toMinutes was added as of Java 1.6.

To add a leading zero for values 0-9, just do:

String.format("%02d min, %02d sec", 
TimeUnit.MILLISECONDS.toMinutes(millis),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
);

If TimeUnit or toMinutes are unsupported (such as on Android before API version 9), use the following equations:

int seconds = (int) (milliseconds / 1000) % 60 ;
int minutes = (int) ((milliseconds / (1000*60)) % 60);
int hours = (int) ((milliseconds / (1000*60*60)) % 24);
//etc...

How to convert milliseconds into a hh:mm:ss.sss timestamp format in R

You can set "%OSn" to give the seconds truncated to n decimal places, where n is between 0 and 6.

format(as.POSIXct(x / 1000, "UTC", origin = "1970-01-01"), "%H:%M:%OS3")

# [1] "00:00:00.029" "00:00:00.300" "00:00:01.000" "00:03:33.450"

Convert Milliseconds to HH:mm:ss format in Angular

Here is a simple version, showing the time in seconds:

Component:

private timer;
private counter: Date;

ngOnInit() {
this.timer = Observable.timer(0,1000)
.subscribe(t => {
this.counter = new Date(0,0,0,0,0,0);
this.counter.setSeconds(t);
});
}

ngOnDestroy(){
this.timer.unsubscribe();
}

Template:

<div class="client-time">
<span>Client time</span><br/>
<strong>{{counter | date:'HH:mm:ss'}} seconds</strong>
</div>

Android : Convert millisecond to time

You could use SimpleDateFormat, but be aware that you should set both the time zone and the locale appropriately:

DateFormat formatter = new SimpleDateFormat("HH:mm:ss", Locale.US);
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
String text = formatter.format(new Date(millis));

The time zone part is important, as otherwise it will use the system-default time zone, which would usually be inappropriate. Note that the Date here will be on January 1st 1970, UTC - assuming your millisecond value is less than 24 hours.

Java: Converting milliseconds to HH:MM:SS

This math will do the trick :

int sec  = (int)(millis/ 1000) % 60 ;
int min = (int)((millis/ (1000*60)) % 60);
int hr = (int)((millis/ (1000*60*60)) % 24);

If you want only Minute and Second, Then :

int sec  = (int)(millis/ 1000) % 60 ;
int min = (int)((millis/ (1000) / 60);


Related Topics



Leave a reply



Submit