打印时间工具类

佐手、 提交于 2020-01-20 01:53:25

 自己写的工具类,方便调试方法运行时间,代码如下:

package com.example.log;

/**
 * User: Jimmy
 * Date: 2020/1/19
 * Desc:
 */

import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class TimeUtils {

    private static Map<String, Long> timeMap = Maps.newHashMap();
    private static DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
    //接入日志组件,当不需要显示打印消息时,不用手动取消
    private static Log log = LogFactory.getLog(TimeUtils.class);
    /**
     * 开始记录
     * @param logName
     */
    public static void startLog(String logName) {
        long startTime = System.currentTimeMillis();
        timeMap.put(startTimeKey(logName), startTime);
        log.info(String.format("[%s] START TIME=>[%s],currentTimeMillis=>%d"
                ,logName
                ,dtf.print(new DateTime(startTime))
                ,startTime));
    }

    /**
     * 开始记录
     */
    public static void startLog() {
        String logName = generatorLogName();
        startLog(logName);
    }

    /**
     * startTimeKey
     * @param logName
     * @return
     */
    private static String startTimeKey(String logName) {
        return Thread.currentThread().getName() + "-" + StringUtils.stripToEmpty(logName);
    }

    /**
     * 开始记录
     */
    public static long endLog() {
        String logName = generatorLogName();
        return endLog(logName);
    }

    /**
     * 结束日志
     * @param logName
     * @return 时间戳
     */
    public static long endLog(String logName) {
        long endTime = System.currentTimeMillis();
        if (!timeMap.containsKey(startTimeKey(logName))) {
            return 0;
        }
        long startTime = timeMap.remove(startTimeKey(logName));
        log.info(String.format("[%s] END TIME=>[%s],currentTimeMillis=>%d"
                ,logName
                ,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date(endTime))
                ,endTime));
        log.info(String.format("[%s] 耗时=>%d ms",logName,endTime - startTime));
        return endTime - startTime;
    }

    private static String generatorLogName() {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        StackTraceElement log = stackTrace[1];
        String tag = null;
        //如果class名称不为自己,则为调用该方法的className.methodName
        for (int i = 1; i < stackTrace.length; i++) {
            StackTraceElement e = stackTrace[i];
            if (!e.getClassName().equals(log.getClassName())) {
                tag = e.getClassName() + "." + e.getMethodName();
                break;
            }
        }
        if (tag == null) {
            tag = log.getClassName() + "." + log.getMethodName();
        }
        return tag;
    }
}

依赖 commons-loggiing,commons-lang3,joda-time,guava,不想依赖可以自己改造。

测试类:

public class Test {
    public static void main(String args[]){
        TimeUtils.startLog();
        TimeUtils.startLog("testTimeUtils");
        for(int i = 1;i<10;i++){
            TimeUtils.startLog("logName" + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            TimeUtils.endLog("logName" + i);
        }
        TimeUtils.endLog("testTimeUtils");
        TimeUtils.endLog();
    }
}

打印结果:

17:29:21.302 [main] INFO com.example.log.TimeUtils - [com.example.log.Test.main] START TIME=>[2020-01-19 17:29:21.184],currentTimeMillis=>1579426161184
17:29:21.308 [main] INFO com.example.log.TimeUtils - [testTimeUtils] START TIME=>[2020-01-19 17:29:21.307],currentTimeMillis=>1579426161307
17:29:21.308 [main] INFO com.example.log.TimeUtils - [logName1] START TIME=>[2020-01-19 17:29:21.308],currentTimeMillis=>1579426161308
17:29:22.309 [main] INFO com.example.log.TimeUtils - [logName1] END TIME=>[2020-01-19 17:29:22.309],currentTimeMillis=>1579426162309
17:29:22.309 [main] INFO com.example.log.TimeUtils - [logName1] 耗时=>1001 ms
17:29:22.310 [main] INFO com.example.log.TimeUtils - [logName2] START TIME=>[2020-01-19 17:29:22.309],currentTimeMillis=>1579426162309
17:29:23.310 [main] INFO com.example.log.TimeUtils - [logName2] END TIME=>[2020-01-19 17:29:23.310],currentTimeMillis=>1579426163310
17:29:23.310 [main] INFO com.example.log.TimeUtils - [logName2] 耗时=>1001 ms
17:29:23.311 [main] INFO com.example.log.TimeUtils - [logName3] START TIME=>[2020-01-19 17:29:23.310],currentTimeMillis=>1579426163310
17:29:24.312 [main] INFO com.example.log.TimeUtils - [logName3] END TIME=>[2020-01-19 17:29:24.312],currentTimeMillis=>1579426164312
17:29:24.312 [main] INFO com.example.log.TimeUtils - [logName3] 耗时=>1002 ms
17:29:24.312 [main] INFO com.example.log.TimeUtils - [logName4] START TIME=>[2020-01-19 17:29:24.312],currentTimeMillis=>1579426164312
17:29:25.312 [main] INFO com.example.log.TimeUtils - [logName4] END TIME=>[2020-01-19 17:29:25.312],currentTimeMillis=>1579426165312
17:29:25.312 [main] INFO com.example.log.TimeUtils - [logName4] 耗时=>1000 ms
17:29:25.313 [main] INFO com.example.log.TimeUtils - [testTimeUtils] END TIME=>[2020-01-19 17:29:25.312],currentTimeMillis=>1579426165312
17:29:25.313 [main] INFO com.example.log.TimeUtils - [testTimeUtils] 耗时=>4005 ms
17:29:25.313 [main] INFO com.example.log.TimeUtils - [com.example.log.Test.main] END TIME=>[2020-01-19 17:29:25.313],currentTimeMillis=>1579426165313
17:29:25.313 [main] INFO com.example.log.TimeUtils - [com.example.log.Test.main] 耗时=>4129 ms

一年又过去了,时间真的很快。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!