黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

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

Java計算兩個時間段的差

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

在本文中,讓我們探索各種方法來找出 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)

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

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 欧美成人一级 | vr欧美乱强伦xxxxx | 国产最强大片免费视频 | 韩国伦理在线免费观看 | 性欧美高清另类videoso | 天天摸夜夜添久久精品麻豆 | 91大片 | 毛片段| 国产综合免费视频 | 首页 动漫 亚洲 欧美 日韩 | avtt天堂网手机版亚洲 | 亚洲成a人片 | 久久99精品视频 | 干日本b | 成人va | 国产一区二区在线观看动漫 | 黄网站在线观看视频 | 久久亚洲网 | 一区二区三区波多野结衣 | 午夜片在线观看 | 天天操天天透 | 最近中文字幕大全高清视频 | 中国一级特黄大片毛片 | 色网站在线观看 | 亚洲国产精品久久网午夜 | 亚洲第一天堂无码专区 | 天天天天天操 | 清风阁视频我爱干 | 国产一级片播放 | 午夜按摩 | 一级毛毛片毛片毛片毛片在线看 | 91视频com| 日本一区二区三区免费在线观看 | 91香蕉国产 | 丁香午夜 | 日本久久综合 | 免费毛片大全 | 成人在线午夜 | 182tv午夜精品视频在线播放 | 中文资源在线观看 | 精品偷国情拍在线视频 |