get Duration Time from long value milliseconds - Android java.util

Android examples for java.util:Second

Description

get Duration Time from long value milliseconds

Demo Code


//package com.java2s;

public class Main {
    public static String getDurationTime(long startTimeInMillis) {
        long currentMillisTime = System.currentTimeMillis()
                - startTimeInMillis;/* w ww  . j  a v  a2  s  . c  om*/
        long totalSeconds = currentMillisTime / 1000;
        int seconds = (int) totalSeconds % 60;
        int minutes = (int) (totalSeconds / 60) % 60;
        int hours = (int) totalSeconds / 3600;
        int days = (int) totalSeconds / (24 * 3600);

        StringBuilder builder = new StringBuilder();
        String minutesSeconds = String
                .format("%02d:%02d", minutes, seconds);
        if (hours > 0) {
            String hoursStr = String.format("%02d", hours);
            builder.append(hoursStr).append(":");
        }

        if (days > 0) {
            String daysStr = String.format("%02d", days);
            builder.append(daysStr).append(":");
        }

        builder.append(minutesSeconds);

        return builder.toString();
    }
}

Related Tutorials