zoukankan      html  css  js  c++  java
  • 时间戳转换为时分秒

    package com.drew.utils;
     
    import java.text.SimpleDateFormat;
    import java.util.Date;
     
    /**
     * long类型数字转换成时分秒毫秒格式
     *
     * @author zero 2019/04/11
     */
    public class ConvertorTime {
     
        public static void main(String[] args) {
            int seconds = 17854, msec = 360000;// 秒,毫秒
            System.out.println(secToTime(seconds));
            System.out.println(msec + "毫秒转换格式时间:	" + msecToTime(msec));
            System.out.println("当前时间(时:分:秒.毫秒)	" + new SimpleDateFormat("HH:mm:ss.SSS").format(new Date()));
        }
     
        /**
         * 秒转换小时-分-秒analytics/util/DateUtil.java
         *
         * @param seconds 秒为单位 比如..600秒
         * @return 比如...2小时3分钟52秒
         */
        public static String secToTime(int seconds) {
            int hour = seconds / 3600;
            int minute = (seconds - hour * 3600) / 60;
            int second = (seconds - hour * 3600 - minute * 60);
     
            StringBuffer sb = new StringBuffer();
            if (hour > 0) {
                sb.append(hour + "小时");
            }
            if (minute > 0) {
                sb.append(minute + "分");
            }
            if (second > 0) {
                sb.append(second + "秒");
            }
            if (second == 0) {
                sb.append("<1秒");
            }
            return sb.toString();
        }
     
        /**
         * 将int类型数字转换成时分秒毫秒的格式数据
         *
         * @param time long类型的数据
         * @return HH:mm:ss.SSS
         * @author zero 2019/04/11
         */
        public static String msecToTime(int time) {
            String timeStr = null;
            int hour = 0;
            int minute = 0;
            int second = 0;
            int millisecond = 0;
            if (time <= 0)
                return "00:00:00.000";
            else {
                second = time / 1000;
                minute = second / 60;
                millisecond = time % 1000;
                if (second < 60) {
                    timeStr = "00:00:" + unitFormat(second) + "." + unitFormat2(millisecond);
                } else if (minute < 60) {
                    second = second % 60;
                    timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second) + "." + unitFormat2(millisecond);
                } else {// 数字>=3600 000的时候
                    hour = minute / 60;
                    minute = minute % 60;
                    second = second - hour * 3600 - minute * 60;
                    timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second) + "."
                        + unitFormat2(millisecond);
                }
            }
            return timeStr;
        }
     
        public static String unitFormat(int i) {// 时分秒的格式转换
            String retStr = null;
            if (i >= 0 && i < 10)
                retStr = "0" + Integer.toString(i);
            else
                retStr = "" + i;
            return retStr;
        }
     
        public static String unitFormat2(int i) {// 毫秒的格式转换
            String retStr = null;
            if (i >= 0 && i < 10)
                retStr = "00" + Integer.toString(i);
            else if (i >= 10 && i < 100) {
                retStr = "0" + Integer.toString(i);
            } else
                retStr = "" + i;
            return retStr;
        }
     
    }
  • 相关阅读:
    Day3-python基础3
    批量分发
    Day2-python基础2
    数据类型总结,and字符编码和文件处理
    集合与字符编码
    列表等及其部分讲解
    赋值符号和循环略解
    Python中常量,和基本数据类型,输入输出的方法,基本运算符。
    Python解释器了解,及部分变量
    计算机硬件,操作系统,编程语言
  • 原文地址:https://www.cnblogs.com/xiaoliu66007/p/15472343.html
Copyright © 2011-2022 走看看