在 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
廣告