zoukankan      html  css  js  c++  java
  • [java]将秒数转化为“天时分秒”的格式(转贴+修改)

    public class Time {
        // format seconds to day hour minute seconds style
        // Exmplae 5000s will be formatted to 1h23m20s
        public static String toDhmsStyle(long allSeconds) {
            String DateTimes = null;
            
            long days = allSeconds / (60 * 60 * 24);
            long hours = (allSeconds % (60 * 60 * 24)) / (60 * 60);
            long minutes = (allSeconds % (60 * 60)) / 60;
            long seconds = allSeconds % 60;
            
            if (days > 0) {
                DateTimes = days + "d" + hours + "h" + minutes + "m" + seconds + "s";
            } else if (hours > 0) {
                DateTimes = hours + "h" + minutes + "m" + seconds + "s";
            } else if (minutes > 0) {
                DateTimes = minutes + "m" + seconds + "s";
            } else {
                DateTimes = seconds + "s";
            }
    
            return DateTimes;
        }
    
        public static void main(String[] args) {
            long mss = 5000;
            String ss = Time.toDhmsStyle(mss);
            System.out.println(ss);
        }
    }

    输出:

    1h23m20s

    参考资料:

    https://blog.csdn.net/weixin_36795952/article/details/78545982

    https://blog.csdn.net/qq_22019789/article/details/74665791

    --END-- 2019年10月5日15:25:39

    改进版:

    函数:

    // change seconds to DayHourMinuteSecond format
        private static String ms2DHMS(long startMs, long endMs) {
            String retval = null;
            long secondCount = (endMs - startMs) / 1000;
            String ms = (endMs - startMs) % 1000 + "ms";
    
            long days = secondCount / (60 * 60 * 24);
            long hours = (secondCount % (60 * 60 * 24)) / (60 * 60);
            long minutes = (secondCount % (60 * 60)) / 60;
            long seconds = secondCount % 60;
    
            if (days > 0) {
                retval = days + "d" + hours + "h" + minutes + "m" + seconds + "s";
            } else if (hours > 0) {
                retval = hours + "h" + minutes + "m" + seconds + "s";
            } else if (minutes > 0) {
                retval = minutes + "m" + seconds + "s";
            } else {
                retval = seconds + "s";
            }
    
            return retval + ms;
        }

    调用:

                long startMs = System.currentTimeMillis();
                clearTable(stmt,conn);
                List<String> insertSqls=generateInsertSqlList();
                betachInsert(insertSqls,stmt,conn);
                long endMs = System.currentTimeMillis();
                log.info("It takes "+ms2DHMS(startMs,endMs)+" to fill "+Total+" records.");

    --END-- 2019年12月22日08:07:20

  • 相关阅读:
    sql: update from
    sql: 查询,select
    english: 遭遇
    sql: sybase与oracle中insert into select和select into的用法
    lcd参数解释及刷新率计算,LCD时序
    Camera Binning Mode
    页框分配器【转】
    (一)洞悉linux下的Netfilter&iptables:什么是Netfilter?
    网络中的NAT模式
    组播、单播、多播
  • 原文地址:https://www.cnblogs.com/heyang78/p/11624813.html
Copyright © 2011-2022 走看看