format Chat Time - Android java.util

Android examples for java.util:Time

Description

format Chat Time

Demo Code


//package com.java2s;

public class Main {

    public static String formatChatTime(int chatTime, boolean showHour) {
        if (chatTime < 1) {
            return "00:00";
        }//from w w w.j a v a 2 s  .  com

        int s = chatTime % 60;
        int m = chatTime / 60;
        if (showHour) {
            m = m % 60;
        }

        int h = chatTime / (60 * 60);

        String secStr = (s >= 10 ? ("" + s) : ("0" + s));
        String minStr = (m >= 10 ? ("" + m) : ("0" + m));
        String hourStr = (h >= 10 ? ("" + h) : ("0" + h));

        if (showHour && h != 0) {
            String separHour = hourStr.trim().equals("") ? "" : ":";
            return hourStr + separHour + minStr + ":" + secStr;
        } else {
            return minStr + ":" + secStr;
        }
    }
}

Related Tutorials