Android开发Android开发Android技术知识

查漏补缺之ParamsUtils(把时间转换成字符串)

2019-03-02  本文已影响2人  Jennyni1122

以下为时间转换成字符串的工具类,可直接引用:

public class ParamsUtils {
    public final static int INVALID = -1;
    public static int getInt(String str){
        int num = INVALID;
        try {
            num = Integer.parseInt(str);
        } catch (NumberFormatException e) {
        }
        return num;
    }
    public static String millsecondsToStr(int seconds){ //把时间转换成字符串
        seconds = seconds / 1000;
        String result = "";
        int hour = 0, min = 0, second = 0;
        hour = seconds / 3600;
        min = (seconds - hour * 3600) / 60;
        second = seconds - hour * 3600 - min * 60;
        if (hour < 10) {
            result += "0" + hour + ":";
        } else {
            result += hour + ":";
        }
        if (min < 10) {
            result += "0" + min + ":";
        } else {
            result += min + ":";
        }
        if (second < 10) {
            result += "0" + second;
        } else {
            result += second;
        }
        return result;
    }
    public static int dpToPx(Context context, int height){ //dp转换成px
        float density = context.getResources().getDisplayMetrics().density;
        height = (int) (height * density + 0.5f);
        return height;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读