在 Java 中计算小时数的经过时间
要获取 Java 中某个操作的小时数的经过时间,我们使用 System.currentTimeMillis() 方法。java.lang.System.currentTimeMillis() 返回当前时间的毫秒数。
声明 − java.lang.System.currentTimeMillis() 的声明如下 −
public static long currentTimeMillis()
该方法返回当前时间与 1970 年 1 月 1 日午夜(UTC 或纪元时间)之间的毫秒时间差。
让我们看一个程序,在 Java 中计算某个操作的小时数的经过时间 −
示例
public class Example {
public static void main(String[] args) throws Exception {
// finding the time before the operation is executed
long start = System.currentTimeMillis();
for (int i = 0; i <5; i++) {
Thread.sleep(60);
}
// finding the time after the operation is executed
long end = System.currentTimeMillis();
// finding the time difference
float msec = end - start;
// converting it into seconds
float sec= msec/1000F;
// converting it into minutes
float minutes=sec/60F;
// converting it into hours
float hours=minutes/60F;
System.out.println(hours + " hours");
}
}输出
8.333334E-5 hours
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP