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

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

Java計算兩個時間段的差

更新時間:2022-10-31 10:01:08 來源:動力節(jié)點 瀏覽2495次

在本文中,讓我們探索各種方法來找出 Java 中兩個時間段之間的差異。為簡單起見,假設(shè)提供給我們的時間段格式為 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 類。通過創(chuàng)建 SimpleDateFormat 對象以 HH:MM:SS 格式解析時間段。SimpleDateFormat 對象解析時間段并返回一個日期對象,該對象可用于計算經(jīng)過的時間。

以下是上述方法的代碼:

// 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 秒。 

時間復(fù)雜度: 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秒。

時間復(fù)雜度: O(1)

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

免費課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 亚洲成a人v大片在线观看 | 国产精品入口麻豆免费观看 | 久久夜夜视频 | 曰批免费视频播放在线看片一 | 亚洲激情视频在线观看 | 欧美成人怡春院在线激情 | 国产短视频精品一区二区三区 | 亚洲视频在线免费播放 | 黄视频在线观看网站 | 在线小毛片 | 日本高清在线播放一区二区三区 | 好男人www社区视频在线 | 免费日本一区 | 91欧美在线| 手机看片欧美日韩 | 免费看的黄视频 | 欧美三级成人 | 国产成人在线视频观看 | 老司机日日摸夜夜摸精品影院 | 免费欧美一级 | 99re这里有免费视频精品 | 一级特级女人18毛片免费视频 | 国产精品天天干 | 免费观看成人毛片 | 1000部禁部视频免费大看 | 一一本大道香蕉大 | 欧美激情精品久久久久久不卡 | 亚洲精品综合久久 | 午夜在线观看视频 | 成人午夜视频免费看欧美 | 成人网18视频网站 | 动漫成年美女黄漫网站在线看 | 免费的污视频 | 国产你懂的在线观看 | 曰批免费视频播放免费完整 | 亚洲第一黄网站 | a国产在线| 欧美日韩在线成人免费 | 窝窝午夜一级看片爽 | 日韩在线精品视频 | 天天色天天射综合网 |