当前位置: 代码网 > it编程>编程语言>Java > Java实现时间戳转代码运行时长

Java实现时间戳转代码运行时长

2025年06月22日 Java 我要评论
一、方法1、代码public static string convert(long timestamp) { if (timestamp <= 0) { return "0s

一、方法

1、代码

public static string convert(long timestamp) {
    if (timestamp <= 0) {
        return "0s";
    }
 
    long onesecond = 1000;
    long oneminute = 60 * onesecond;
    long onehour = 60 * oneminute;
    long oneday = 24 * onehour;
 
    int d = 0;
    int h = 0;
    int m = 0;
    int s = 0;
 
    string result = "";
    if (timestamp >= oneday) {
        d = (int) (timestamp / oneday);
        result += d + "d";
    }
    timestamp -= d * oneday;
 
    if (timestamp >= onehour) {
        h = (int) (timestamp / onehour);
        result += h + "h";
    }
    timestamp -= h * onehour;
 
    if (timestamp >= oneminute) {
        m = (int) (timestamp / oneminute);
        result += m + "m";
    }
    timestamp -= m * oneminute;
 
    if (timestamp >= onesecond) {
        s = (int) (timestamp / onesecond);
        result += s + "s";
    }
    timestamp -= s * onesecond;
 
    if (timestamp > 0) {
        result += timestamp + "ms";
    }
 
    return result;
}

2、使用示例

public static void main(string[] args) throws interruptedexception {
    long starttime = system.currenttimemillis();
    
    // 模拟业务代码运行时间
    thread.sleep(500);
 
    long endtime = system.currenttimemillis();
    system.out.println(convert(endtime - starttime));
}
public static void main(string[] args) throws interruptedexception {
    long starttime = system.currenttimemillis();
    simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss.s");
    system.out.println("[开始时间] " + sdf.format(starttime));
 
    // 模拟业务代码运行时间
    thread.sleep(500);
 
    long endtime = system.currenttimemillis();
    system.out.println("[结束时间] " + sdf.format(endtime));
    system.out.println("[运行时间] " + convert(endtime - starttime));
}

二、工具类

如果需要在多处使用,并且开始时间和结束时间都要打印,会有点繁琐。为了便捷使用,设置了一个工具类。

1、代码

 
import java.text.simpledateformat;
import java.util.arraylist;
import java.util.list;
 
/**
 * print time-consuming(eg: 1h23m20s50ms). <br/>
 * -step1: addstarttime() <br/>
 * -step2: addendtime() <br/>
 * -step3: print() <br/>
 * steps 1 to 3 are a set of operations that can be looped. calling addstarttime() and addendtime()
 * will print the current time in the format of "yyyy-mm-dd hh:mm:ss.s". the method addstarttime()
 * and addendtime() must match each other, otherwise output null.
 */
public class timeconsume {
    private final static simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss.s");
    private final list<long> starttime;
    private final list<long> endtime;
    private int point;
 
    public timeconsume() {
        this.starttime = new arraylist<>();
        this.endtime = new arraylist<>();
        this.point = 0;
    }
 
    /***
     * print current time(simple date format is "yyyy-mm-dd hh:mm:ss.s") and add current time to
     * starttime.
     */
    public void addstarttime() {
        long currenttimemillis = system.currenttimemillis();
        system.out.println("[start time] " + sdf.format(currenttimemillis));
        this.starttime.add(currenttimemillis);
    }
 
    /***
     * print current time(simple date format is "yyyy-mm-dd hh:mm:ss.s") and add current time to
     * endtime.
     */
    public void addendtime() {
        long currenttimemillis = system.currenttimemillis();
        system.out.println("[end time] " + sdf.format(currenttimemillis));
        this.endtime.add(currenttimemillis);
    }
 
    /***
     * print time-consuming(eg: 1h23m20s50ms). if the start time and end time do not match each
     * other, output empty. this method can be reused.
     */
    public void print() {
        try {
            system.out.println("[time-consuming] "
                    + convert(this.endtime.get(point) - this.starttime.get(point)));
        } catch (exception e) {
            system.out.println("[time-consuming] null");
        }
        this.point++;
    }
 
    /***
     * timestamp convert to time-consuming
     * 
     * @param timestamp long timestamp
     * @return time-consuming string, eg: 1d, 12h32m45s123ms, 1h23m20s50ms, 5min2s, 10s, 520ms
     */
    public string convert(long timestamp) {
        if (timestamp <= 0) {
            return "0s";
        }
 
        long onesecond = 1000;
        long oneminute = 60 * onesecond;
        long onehour = 60 * oneminute;
        long oneday = 24 * onehour;
 
        int d = 0;
        int h = 0;
        int m = 0;
        int s = 0;
 
        string result = "";
        if (timestamp >= oneday) {
            d = (int) (timestamp / oneday);
            result += d + "d";
        }
        timestamp -= d * oneday;
 
        if (timestamp >= onehour) {
            h = (int) (timestamp / onehour);
            result += h + "h";
        }
        timestamp -= h * onehour;
 
        if (timestamp >= oneminute) {
            m = (int) (timestamp / oneminute);
            result += m + "m";
        }
        timestamp -= m * oneminute;
 
        if (timestamp >= onesecond) {
            s = (int) (timestamp / onesecond);
            result += s + "s";
        }
        timestamp -= s * onesecond;
 
        if (timestamp > 0) {
            result += timestamp + "ms";
        }
 
        return result;
    }
}

2、使用示例

public static void main(string[] args) throws interruptedexception {
    timeconsume tc = new timeconsume();
    tc.addstarttime();
 
    // 模拟业务代码运行时间
    thread.sleep(500);
 
    tc.addendtime();
    tc.print();
 
    // 可多次调用
    tc.addstarttime();
    // 模拟业务代码运行时间
    thread.sleep(50);
    tc.addendtime();
    tc.print();
}

到此这篇关于java实现时间戳转代码运行时长的文章就介绍到这了,更多相关java时间戳转代码运行时长内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com