Java-统计程序运行的时长(计算两个时间相差的毫秒数、秒数)
最近在做Hbase的查询性能验证,需要统计查询的执行时长,所以需要统计开始时间和结束时间的时间差。
下面是使用SimpleDateFormat和Date计算时间差(相差毫秒数)的程序示例,仅供参考。
package com.sgcc; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; public class Main { public static long timeDiff(String startTime, String endTime) {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
long diff;
try {
diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
} catch (ParseException e) {
throw new RuntimeException(e);
}
return diff;
} public static void main(String[] args) { SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String startTime = sdf.format(new Date());
try {
Thread.sleep(1200);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
String endTime = sdf.format(new Date());
System.out.println(timeDiff(startTime,endTime)+"ms"); }
}
【改进版本】时间差可以自动按照时间单位(小时、分钟、秒、毫秒)显示,四舍五入保留2位小数。
package com.sgcc; import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; public class Main { public static String timeDiff(String startTime, String endTime,String timeUnit) {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
DecimalFormat df = new DecimalFormat("#.00");
long diff;
try {
diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
} catch (ParseException e) {
throw new RuntimeException(e);
}
if (timeUnit.equals("h")){
double diffhour = Double.parseDouble(df.format(diff / (1000.0*3600)));
return diffhour + "h";
}
else if (timeUnit.equals("min")){
double diffmin = Double.parseDouble(df.format(diff / (1000.0*60)));
return diffmin + "min";
} else if(timeUnit.equals("s")){
double diffSeconds = Double.parseDouble(df.format(diff / 1000.0));
return diffSeconds + "s";
}else if(timeUnit.equals("ms")){
return diff + "ms";
}else{
System.out.println("time unit is wrong! The default result returns millisecond.");
return diff + "ms";
}
} public static void main(String[] args) { SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String startTime = sdf.format(new Date());
try {
Thread.sleep(1599);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
String endTime = sdf.format(new Date()); System.out.println(timeDiff(startTime,endTime,"s")); }
}
Java-统计程序运行的时长(计算两个时间相差的毫秒数、秒数)的更多相关文章
- 计算两个NSDate之间,相隔多少秒数
计算两个NSDate之间,相隔多少秒数 //两个时间间隔秒数 - (NSInteger)intervalSecondsWithSmallDate:(NSDate*)smallDate bigDate: ...
- Java计算两个时间的天数差与月数差 LocalDateTime
/** * 计算两个时间点的天数差 * @param dt1 第一个时间点 * @param dt2 第二个时间点 * @return int,即要计算的天数差 */ public stat ...
- MySQL计算两个日期相差的天数、月数、年数
MySQL自带的日期函数TIMESTAMPDIFF计算两个日期相差的秒数.分钟数.小时数.天数.周数.季度数.月数.年数,当前日期增加或者减少一天.一周等等. SELECT TIMESTAMPDIFF ...
- java计算两个时间相差(天、小时、分钟、秒)
public static Long dateDiff(String startTime, String endTime, String format, String str) { // 按照传入的格 ...
- php计算两个时间相差的天数、小时数、分钟数、秒数
$startdate="2011-3-15 11:50:00";//开始时间 $enddate="2012-12-12 12:12:12";//结束时间 $da ...
- java统计程序运行的时间
耗时统计 第一种是以毫秒为单位计算的.long startTime = System.currentTimeMillis(); //获取开始时间 //程序做一些功能性的操作doSomething ...
- JAVA中计算两个时间相差多少 天,时,分,秒
1: import java.util.Date; 2: 3: public class ShowTimeInterval{ 4: public void ShowTimeInterval(Date ...
- db2 怎么计算两个时间相差多少个月。如2015-10-10 和2014-1-12
SELECT timestampdiff (256, char(timestamp('2013-12-30 20:30:30') - timestamp('2001-09-26 15:24:23')) ...
- SqlSever基础 datediff 计算两个时间相差多少年份
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- sql server 计算两个时间 相差的 几天几时几分几秒
CAST ( CAST ( DATEDIFF ( ss, StartTime, ConcludeTime ) / ( 60 * 60 * 24 ) AS INT ) AS VARCHAR ) + '天 ...
随机推荐
- python之pycharm常见使用技巧
一.ctrl+d:复制
- spring注入的几种方式
本文为博主原创,未经允许不得转载: Spring注入有以下几种方式: 构造方法注入:通过构造方法实现依赖注入.在类的构造方法中使用@Autowired注解注入需要的依赖类. Setter方法注入:通过 ...
- 15-Verilog Coding Style
Verilog Coding Style 1.为什么需要Coding Style 可综合性 - 代码需要综合成网表,如果写了一些不可综合的代码,会出现错误 可读性,代码通常有多个版本,所以需要保证代码 ...
- Linux 查看office文件及pdf文件
1.查看pdf文件 evince PdfFile_name 查看office文件 openoffice.org 文件名 & // 打开或者编辑.doc.odt等文本文档命令 openoffic ...
- cout对象在全局只能拥有一个
1.问题 在学习符号重载的过程中,有一个想法 std::ostream& operator<<(std::ostream &cout, Person &p); 中s ...
- Git Clone一个GitHub仓库时,发生报错
1.问题 1.使用HTTP方式:Git: fatal: unable to access ' https://github. com/Light-City/CPlusPlusThings. git/' ...
- STM32CubeMX教程23 FSMC - IS62WV51216(SRAM)驱动
1.准备材料 开发板(正点原子stm32f407探索者开发板V2.4) STM32CubeMX软件(Version 6.10.0) 野火DAP仿真器 keil µVision5 IDE(MDK-Arm ...
- [转帖]MySQL数据库8.0.29-8.0.31版本使用 INSTANT 算法新增字段bug
https://www.cnblogs.com/harda/p/17528512.html xxx下发MySQL数据库共性隐患排查通知,要求统一排查MySQL数据库8.0.29及以后版本使用 INST ...
- [转帖]一个小操作,SQL 查询速度翻了 1000 倍
https://tidb.net/book/tidb-monthly/2022/2022-04/usercase/sql-1000 背景介绍 某一天早上来到公司,接到业务同学反馈,线上某个SQL之前 ...
- kafka的学习之一_带SASL鉴权的集群安装与启动
kafka的学习之一_带SASL鉴权的集群安装与启动 背景 想开始一段新的里程. 可能会比现在累, 可能会需要更多的学习和努力. kafka可能就是其中之一. 自己之前总是畏缩不前. 不想面对很多压力 ...