【Java】时间戳转耗时时长

news/2025/3/31 6:30:05/

目录

一、方法

1、代码

2、使用示例

二、工具类

1、代码

2、使用示例

三、项目地址

1、GitHub

2、Gitee


一、方法

1、代码

java">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、使用示例

java">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));
}
java">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、代码

java">
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、使用示例

java">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();
}

三、项目地址

1、GitHub

cxzgwing/toolkit

2、Gitee

cxzgwing/toolkit


http://www.ppmy.cn/news/1583774.html

相关文章

HTML+CSS简单实现小太阳

成果&#xff1a; 代码&#xff1a; HTML <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>太阳照常升起</title><link rel"stylesheet" href"./css/style.css"> &l…

【FW】ADB指令分类速查清单

1. 设备管理 指令核心作用adb devices列出已连接设备adb reboot重启设备adb reboot bootloader进入Bootloader模式adb reboot recovery进入Recovery模式adb root获取Root权限&#xff08;需设备支持&#xff09;adb remount挂载系统分区为可读写 2. 应用管理 指令核心作用adb…

npm install 卡在创建项目:sill idealTree buildDeps

参考&#xff1a; https://blog.csdn.net/PengXing_Huang/article/details/136460133 或者再执行 npm install -g cnpm --registryhttps://registry.npm.taobao.org 或者换梯子

如何判断一块OLED显示屏幕好坏?

买手机或者平板的时候&#xff0c;屏幕好坏是一个非常重要考虑因素&#xff0c;从客户角度如何感知一块屏幕好坏呢&#xff1f; 个人在做显示领域相关工作&#xff0c;进行总结一下&#xff08;讨论主要集中手机平板这类中小型尺寸OLED的屏幕&#xff09;。 文章目录 一、中小…

批量给 PDF 页面添加超链接

让我们的 PDF 文档上传到互联网的时候&#xff0c;我们可能需要对其做一些处理&#xff0c;比如说我们希望别人在点击 PDF 文档页面的时候就跳转到指定的链接&#xff0c;那我们就需要给 PDF 文档的页面上添加链接。今天就给大家介绍一下如何同时对多个 PDF 文档的所有页面添加…

神经网络基础(NN)

一、神经网络是什么&#xff1f; 神经网络&#xff08;Neural Network, NN&#xff09;是机器学习中模仿生物神经系统结构的计算模型。它由大量人工神经元通过权重连接构成&#xff0c;能够通过数据学习复杂的非线性关系。 生物类比&#xff1a;神经元接收输入信号&#xff08;…

213.SpringSecurity:授权,授权实战,OAuth2,SpringSecurity中OAuth2认证服务器、资源服务器搭建,JWT

目录 一、授权 1.授权的核心概念 2.权限管理策略 3.基于过滤器(URL)的权限管理实战 (1)创建项目 (2)基于过滤器(URL)访问 (3)常用方法 (4)antMathers,mvcMathers,regexMathers区别 4.基于 方法 权限管理 (1)使用场景 (2)使用方法 (3)具体实现 二、…

STM32--SPI通信讲解

前言 嘿&#xff0c;小伙伴们&#xff01;今天咱们来聊聊STM32的SPI通信。SPI&#xff08;Serial Peripheral Interface&#xff09;是一种超常用的串行通信协议&#xff0c;特别适合微控制器和各种外设&#xff08;比如传感器、存储器、显示屏&#xff09;之间的通信。如果你…