Java 计算两个时间段的差异
要在Java中处理日期和时间,我们需要导入java.time、java.util和java.text包。我们将使用这些包提供的以下类和方法来计算Java中两个时间段之间的差异-
- SimpleDateFormat类
-
Date类
-
LocalDate类
-
Period类
-
parse()方法
-
between()方法
随着我们在本文中的进一步探讨,我们将了解这些类和方法的用途。
方法1:使用SimpleDateFormat和Date类
SimpleDateFormat - 它是Java中的一个类,允许我们将日期转换为字符串(格式化)和将字符串转换为日期(解析)。
Date - 它是一个表示一段特定时间的类(以毫秒为单位)。
示例
import java.text.*;
import java.util.Date;
public class Main {
public static void main(String[] args) throws Exception {
String timePeriod1 = "09:00:00";
String timePeriod2 = "10:20:00";
SimpleDateFormat timeformat = new SimpleDateFormat("HH:mm:ss");
Date dat1 = timeformat.parse(timePeriod1);
Date dat2 = timeformat.parse(timePeriod2);
long diffInMs = dat2.getTime() - dat1.getTime();
long diffInSec = diffInMs / 1000 % 60;
long diffInMin = diffInMs / (60 * 1000) % 60;
long diffInHr = diffInMs / (60 * 60 * 1000) % 24;
System.out.format("Difference between these two time periods is: " + diffInHr +" hours " + diffInMin +" minutes " + diffInSec + " seconds");
}
}
输出
Difference between these two time periods is: 1 hours 20 minutes 0 seconds
在上面的代码中,我们使用了格式为HH:mm:ss的两个时间段。我们创建了一个名为timeformat的SimpleDateFormat类对象。为了将文本从字符串转换为给定格式的日期,我们使用了parse()方法。差异包括年份和日期。
示例
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main{
public static void main(String[] args) throws Exception {
String timePeriod1 = "23/02/23 09:25:00";
String timePeriod2 = "24/03/23 09:00:00";
SimpleDateFormat timeformat = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
Date dat1 = timeformat.parse(timePeriod1);
Date dat2 = timeformat.parse(timePeriod2);
long diffInMs = dat2.getTime() - dat1.getTime();
long diffInSec = diffInMs / 1000 % 60;
long diffInMin = diffInMs / (60 * 1000) % 60;
long diffInHr = diffInMs / (60 * 60 * 1000) % 24;
long diffInDays = diffInMs / (60 * 60 * 24 * 1000) % 365;
long diffInYears = (diffInMs / (1000l*60*60*24*365));
System.out.format("Difference between these two time periods is: " + diffInYears + " Years " + diffInDays + " Days " + diffInHr + " hours " + diffInMin + " minutes " + diffInSec + " seconds");
}
}
输出
Difference between these two time periods is: 1 Years 28 Days 23 hours 35 minutes 0 seconds
在同一个程序中,我们已经计算了包括年份和日期在内的时间段的差异。
方法2:使用LocalDate和Period类
LocalDate - 它是一个不可变的日期时间对象,用于表示日期。其默认格式为yyyy-MM-dd。它不能用于存储基于时间的值,它只描述日期。
Period - 这个类存在于java.time包中。它只使用基于日期的值。
示例
import java.time.*;
import java.util.*;
public class Main {
public static void main(String[] args){
LocalDate d1 = LocalDate.of(2023, 11, 20);
LocalDate d2 = LocalDate.of(2022, 02, 01);
Period diff = Period.between(d2, d1);
System.out.printf("Difference between these two time periods is: " + diff.getYears() +" years " + diff.getMonths() + " months " + diff.getDays() + " days");
}
}
输出
Difference between these two time periods is: 1 years 9 months 19 days
在这个方法中,我们使用yyyy,MM,dd格式计算了两个时间段之间的差异。我们创建了一个Period类的’diff’对象来存储差异。使用Period类的内置方法’between()’来找到两个日期之间的差异。它接受两个参数,即开始和结束日期。
当我们改变格式时
示例
import java.time.*;
import java.util.*;
public class Main {
public static void main(String[] args){
LocalDate d1 = LocalDate.parse("2021-01-20");
LocalDate d2 = LocalDate.parse("2023-04-01");
Period diff = Period.between(d1, d2);
System.out.printf("Difference between these two time periods is: " + diff);
}
}
输出
Difference between these two time periods is: P2Y2M12D
在上面的程序中,我们已经将日期格式更改为yyyy-MM-dd。
结论
我们已经了解了java的SimpleDateFormat、Date、LocalDate和Period类的用法,也了解了如何使用parse()和between()方法,在java编程语言中处理日期和时间时非常重要和有用。