get Time String From Milliseconds - Android java.util

Android examples for java.util:Millisecond

Description

get Time String From Milliseconds

Demo Code


//package com.java2s;

public class Main {
    public static String getTimeStringFromMilliseconds(long milliseconds) {
        String seconds = Integer
                .toString(getSecondsFromMilliseconds(milliseconds));
        String minutes = Integer
                .toString(getMinutesFromMilliseconds(milliseconds));
        String hours = Integer
                .toString(getHoursFromMilliseconds(milliseconds));
        for (int i = 0; i < 2; i++) {
            if (seconds.length() < 2) {
                seconds = "0" + seconds;
            }/*from ww w  .j  a v  a2  s.  com*/
            if (minutes.length() < 2) {
                minutes = "0" + minutes;
            }
            if (hours.length() < 2) {
                hours = "0" + hours;
            }
        }
        return hours + ":" + minutes + ":" + seconds;
    }

    public static int getSecondsFromMilliseconds(long milliseconds) {
        long time = milliseconds / 1000;
        return (int) (time % 60);
    }

    public static int getMinutesFromMilliseconds(long milliseconds) {
        long time = milliseconds / 1000;
        return (int) ((time % 3600) / 60);
    }

    public static int getHoursFromMilliseconds(long milliseconds) {
        long time = milliseconds / 1000;
        return (int) (time / 3600);
    }
}

Related Tutorials