最近在做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-统计程序运行的时长(计算两个时间相差的毫秒数、秒数)的更多相关文章

  1. 计算两个NSDate之间,相隔多少秒数

    计算两个NSDate之间,相隔多少秒数 //两个时间间隔秒数 - (NSInteger)intervalSecondsWithSmallDate:(NSDate*)smallDate bigDate: ...

  2. Java计算两个时间的天数差与月数差 LocalDateTime

    /**  * 计算两个时间点的天数差  * @param dt1 第一个时间点  * @param dt2 第二个时间点  * @return int,即要计算的天数差  */ public stat ...

  3. MySQL计算两个日期相差的天数、月数、年数

    MySQL自带的日期函数TIMESTAMPDIFF计算两个日期相差的秒数.分钟数.小时数.天数.周数.季度数.月数.年数,当前日期增加或者减少一天.一周等等. SELECT TIMESTAMPDIFF ...

  4. java计算两个时间相差(天、小时、分钟、秒)

    public static Long dateDiff(String startTime, String endTime, String format, String str) { // 按照传入的格 ...

  5. php计算两个时间相差的天数、小时数、分钟数、秒数

    $startdate="2011-3-15 11:50:00";//开始时间 $enddate="2012-12-12 12:12:12";//结束时间 $da ...

  6. java统计程序运行的时间

    耗时统计 第一种是以毫秒为单位计算的.long startTime = System.currentTimeMillis();    //获取开始时间 //程序做一些功能性的操作doSomething ...

  7. JAVA中计算两个时间相差多少 天,时,分,秒

    1: import java.util.Date; 2: 3: public class ShowTimeInterval{ 4: public void ShowTimeInterval(Date ...

  8. 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')) ...

  9. SqlSever基础 datediff 计算两个时间相差多少年份

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  10. sql server 计算两个时间 相差的 几天几时几分几秒

    CAST ( CAST ( DATEDIFF ( ss, StartTime, ConcludeTime ) / ( 60 * 60 * 24 ) AS INT ) AS VARCHAR ) + '天 ...

随机推荐

  1. freeswitch的事件引擎实现分析

    概述 freeswitch是由事件驱动的,fs内部有各种事件来标识状态的变化包括呼叫的变化.配置的变化.号码的变化等等. 而一个框架内的事件引擎需要实现哪些基本的功能呢? 让我们来看一下fs的事件引擎 ...

  2. Redis 中bitMap使用及实现访问量

    1. Bitmap 是什么 Bitmap(也称为位数组或者位向量等)是一种实现对位的操作的'数据结构',在数据结构加引号主要因为: Bitmap 本身不是一种数据结构,底层实际上是字符串,可以借助字符 ...

  3. nginx.conf 配置解析及常用配置

    本文为博主原创,未经允许不得转载: nginx.conf 配置文件配置解析 #定义 Nginx 运行的用户和用户组.默认nginx的安装用户为 nobody user www www: #启动进程,通 ...

  4. 基于Tensorflow技术开发的计算机毕业设计辅助生成器(使用AI大模型技术)

    这是一个辅助生成计算机毕业设计的工具,可以自动完成毕业设计的源码.它基于几百个github上面开源的java和python项目,运用tensorflow技术,训练出了AI大模型.基本实现了计算机毕业设 ...

  5. 无法访问Github仓库的极简治标法

    技术背景 由于IP原因,国内的IP访问Github仓库的时候会经常遇到一些困难,甚至存在无法Ping通的情况.尝试过FastGithub等方案,但还是会遇到各种各样的问题,导致情况越来越复杂. 但是转 ...

  6. [转帖]nginx 反向代理 URL替换方案

    nginx 提供反向代理服务,日常开发过程中有时候我们需要使用nginx 作为代理服务根据url的不同去访问不同的服务器或者不同端口,如下提供两种方案. 1.直接替换location  匹配部分 1. ...

  7. [转帖]Linux-文本处理三剑客grep详解

    https://developer.aliyun.com/article/885611?spm=a2c6h.24874632.expert-profile.311.7c46cfe9h5DxWK 简介: ...

  8. js加减乘除运算出现精度丢失

    做乘法运算出现精度丢失 let aa= 2106.49 console.log( aa*10000 ) //21064899.999999996 console.log( Math.round(aa* ...

  9. x509: cannot validate certificate for xxx because it doesn't contain any IP SANs

    项目中有时候需要访问https网站,但如果该网站使用的是自建证书,那client端验证server端证书时,有时候会报错: x509: cannot validate certificate for ...

  10. 根据TxID获取上链数据

    根据TxID获取上链信息 前段时间应甲方爸爸的要求,需要在现有的业务系统中新增一个根据TxID来查询上链信息的接口.搜了一圈发现相关的信息很少,最后只能祭出终极大招:Read Source Code. ...