Java實(shí)現(xiàn)時(shí)間戳轉(zhuǎn)代碼運(yùn)行時(shí)長
一、方法
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(); // 模擬業(yè)務(wù)代碼運(yùn)行時(shí)間 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("[開始時(shí)間] " + sdf.format(startTime)); // 模擬業(yè)務(wù)代碼運(yùn)行時(shí)間 Thread.sleep(500); long endTime = System.currentTimeMillis(); System.out.println("[結(jié)束時(shí)間] " + sdf.format(endTime)); System.out.println("[運(yùn)行時(shí)間] " + convert(endTime - startTime)); }
二、工具類
如果需要在多處使用,并且開始時(shí)間和結(jié)束時(shí)間都要打印,會(huì)有點(diǎn)繁瑣。為了便捷使用,設(shè)置了一個(gè)工具類。
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(); // 模擬業(yè)務(wù)代碼運(yùn)行時(shí)間 Thread.sleep(500); tc.addEndTime(); tc.print(); // 可多次調(diào)用 tc.addStartTime(); // 模擬業(yè)務(wù)代碼運(yùn)行時(shí)間 Thread.sleep(50); tc.addEndTime(); tc.print(); }
到此這篇關(guān)于Java實(shí)現(xiàn)時(shí)間戳轉(zhuǎn)代碼運(yùn)行時(shí)長的文章就介紹到這了,更多相關(guān)Java時(shí)間戳轉(zhuǎn)代碼運(yùn)行時(shí)長內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot用@Async注解實(shí)現(xiàn)異步任務(wù)
這篇文章主要介紹了SpringBoot用@Async注解實(shí)現(xiàn)異步任務(wù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12SpringBoot日志配置SLF4J和Logback的方法實(shí)現(xiàn)
日志記錄是不可或缺的一部分,本文主要介紹了SpringBoot日志配置SLF4J和Logback的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04mybatis通過if語句實(shí)現(xiàn)增刪改查操作
這篇文章主要介紹了mybatis通過if語句實(shí)現(xiàn)增刪改查操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11一文詳解SpringBoot使用Kafka如何保證消息不丟失
這篇文章主要為大家詳細(xì)介紹了SpringBoot使用Kafka如何保證消息不丟失的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下2025-01-01SpringBoot項(xiàng)目實(shí)現(xiàn)jar包方式打包部署
SpringBoot默認(rèn)的打包方式就是jar包,本文就來介紹一下SpringBoot項(xiàng)目實(shí)現(xiàn)jar包方式打包部署,具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08SSH框架網(wǎng)上商城項(xiàng)目第19戰(zhàn)之訂單信息級聯(lián)入庫以及頁面緩存問題
這篇文章主要介紹了SSH框架網(wǎng)上商城項(xiàng)目第19戰(zhàn)之訂單信息級聯(lián)入庫以及頁面緩存問題,感興趣的小伙伴們可以參考一下2016-06-06