大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學習攻略 Java學習 Java計算兩個時間段的差

Java計算兩個時間段的差

更新時間:2022-10-31 10:01:08 來源:動力節點 瀏覽2463次

在本文中,讓我們探索各種方法來找出 Java 中兩個時間段之間的差異。為簡單起見,假設提供給我們的時間段格式為 HH:MM:SS

例子

輸入:第一個時間段:- 18:00:00
    第二時間段:- 21:00:00
輸出: 3小時0分0秒
輸入:第一個時間段:- 17:00:00
        第二時間段:- 23:22:00
輸出: 6小時22分0秒

方法 1 :- 使用 SimpleDateFormat 類和 Date 類

JDK 第 7 版的 java.text 包中添加了 SimpleDateFormat 類。通過創建 SimpleDateFormat 對象以 HH:MM:SS 格式解析時間段。SimpleDateFormat 對象解析時間段并返回一個日期對象,該對象可用于計算經過的時間。

以下是上述方法的代碼:

// Java Program to Find the difference
// between Two Time Periods
// Importing the Date Class from the util package
import java.util.*;
// Importing the SimpleDateFormat
// Class from the text package
import java.text.*;
public class GFG {
	public static void main(String[] args) throws Exception
	{
		// Dates to be parsed
		String time1 = "18:00:00";
		String time2 = "7:30:50";
		// Creating a SimpleDateFormat object
		// to parse time in the format HH:MM:SS
		SimpleDateFormat simpleDateFormat
			= new SimpleDateFormat("HH:mm:ss");
		// Parsing the Time Period
		Date date1 = simpleDateFormat.parse(time1);
		Date date2 = simpleDateFormat.parse(time2);
		// Calculating the difference in milliseconds
		long differenceInMilliSeconds
			= Math.abs(date2.getTime() - date1.getTime());
		// Calculating the difference in Hours
		long differenceInHours
			= (differenceInMilliSeconds / (60 * 60 * 1000))
			% 24;
		// Calculating the difference in Minutes
		long differenceInMinutes
			= (differenceInMilliSeconds / (60 * 1000)) % 60;
		// Calculating the difference in Seconds
		long differenceInSeconds
			= (differenceInMilliSeconds / 1000) % 60;
		// Printing the answer
		System.out.println(
			"Difference is " + differenceInHours + " hours "
			+ differenceInMinutes + " minutes "
			+ differenceInSeconds + " Seconds. ");
	}
}

輸出

時差是 10 小時 29 分 10 秒。 

時間復雜度: O(1)

方法 2 :- 使用 LocalTime 和 ChronoUnit 類

Java 在第 8 版 JDK 中帶來了大量特性,其中很少有 java.time 包中的 LocalTime 和 ChronoUnit 類。LocalTime 對象以 HH:MM:SS 格式解析日期,而 ChronoUnit 用于獲取小時、分鐘和秒的差異。

以下是上述方法的代碼:

// Java program to get the difference
// between Two Time Periods in Java
// Importing the LocalTime class
import java.time.*;
// Importing the ChronoUnit class
import java.time.temporal.ChronoUnit;
class GFG {
	public static void main(String[] args)
	{
		// Parsing Time Period in the format HH:MM:SS
		LocalTime time1 = LocalTime.of(18, 00, 00);
		LocalTime time2 = LocalTime.of(21, 22, 00);
		// Calculating the difference in Hours
		long hours = ChronoUnit.HOURS.between(time1, time2);
		// Calculating the difference in Minutes
		long minutes
			= ChronoUnit.MINUTES.between(time1, time2) % 60;
		// Calculating the difference in Seconds
		long seconds
			= ChronoUnit.SECONDS.between(time1, time2) % 60;
		// Printing the difference
		System.out.println(
			"Difference is " + hours + " hours " + minutes
			+ " minutes " + seconds + " seconds.");
	}
}

輸出

相差3小時22分0秒。

時間復雜度: O(1)

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 日韩成人免费一级毛片 | 久久一日本道色综合久久 | 亚洲一区视频在线播放 | 亚洲qingse | 久久天天躁狠狠躁夜夜爽蜜月 | 久久久亚洲精品视频 | 四虎最新地址 | 一区二区三区四区在线观看视频 | 亚洲精品久久9热 | 亚洲午夜一区 | 久久精品动漫99精品动漫 | 免费观看国产精品视频 | 久久免费高清 | 国产精品免费看久久久麻豆 | 色播影音 | 亚洲国产精品日韩高清秒播 | 亚洲第一欧美 | 神马影院在线观看我不卡 | 色老成人精品视频在线观看 | 福利视频久久 | 欧美一级别 | 手机看片福利盒子久久青 | 欧美日韩亚洲精品国产色 | 国产成人一区免费观看 | 久久久亚洲国产精品主播 | 欧美成人鲁丝片在线观看 | 日韩欧美日本 | 麻豆久久婷婷国产综合五月 | 日日做夜夜爱 | 深夜久久| 俄罗斯毛片免费大全 | 精品国产成人 | 色综合久久久久 | 久久99精品福利久久久 | 免费精品国产自产拍观看 | 久草精品视频 | 国产精品久久久久久久免费大片 | 五月开心婷婷 | 日本精品欧洲www | 青青热久免费精品视频精品 | 91在线免费视频观看 |