generate Time like 20s --> 02:00 - Android java.util

Android examples for java.util:Date Time

Description

generate Time like 20s --> 02:00

Demo Code

import android.text.TextUtils;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    /**//from   ww w .  j  a  v a2  s  .  c om
     
     * @param milliseconds
     * @return
     */
    public static String generateTime(long milliseconds) {
        StringBuilder sb = new StringBuilder();
        long h, m, s;
        h = milliseconds / (3600000);
        if (h > 0) {
            sb.append(h + ":");
        }
        m = (milliseconds - h * 3600000) / 60000;
        if (m < 10) {
            sb.append("0" + m + ":");
        } else {
            sb.append(m + ":");
        }
        s = milliseconds % 60000 / 1000;
        if (s < 10) {
            sb.append("0" + s);
        } else {
            sb.append("" + s);
        }
        return sb.toString();
    }

}

Related Tutorials