get Time in format 00:00:00 from int value - Android java.util

Android examples for java.util:Date Time

Description

get Time in format 00:00:00 from int value

Demo Code

import android.util.Log;
import java.io.File;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main{

    public static String getTime(int ms) {
        String time = "";
        String second = "";
        String minute = "";
        String hours = "";

        int total_sec = ms / 1000; 
        int total_min = total_sec / 60; 
        int total_hour = total_min / 60; 
        int sec = total_sec % 60; 
        int min = total_min % 60;
        int hour = total_hour % 24;

        if (sec < 10) {
            second = "0" + sec;
        } else {/*from w w  w . j a v  a2s .  com*/
            second = sec + "";
        }

        if (min < 10) {
            minute = "0" + min;
        } else {
            minute = min + "";
        }

        time = minute + ":" + second;
        if (hour > 0) {
            time = hours + ":" + minute + ":" + second;
        }
        return time;
    }

}

Related Tutorials