Java程序计算两个时间段之间的差值
给定两个时间段,我们的任务是编写一个Java程序来计算它们之间的差值。对于这个问题,我们可以使用java.time、java.util和java.text包的类和方法。
- SimpleDateFormat 类
- Date 类
- LocalDate 类
- Period 类
- parse() 方法
- between() 方法
随着我们进一步阅读本文,我们将了解这些类和方法在计算两个时间段之间差值中的用途。
使用 SimpleDateFormat 和 Date 类
SimpleDateFormat是Java中的一个类,它允许我们以本地格式将日期转换为字符串(格式化)并将字符串转换为日期(解析)。Date是一个Java类,表示某个时间段(以毫秒为单位)。
示例 1
在下面的代码中,我们使用了HH:mm:ss格式的两个时间段。为了根据给定的格式将字符串中的文本转换为日期,我们使用了parse()方法。
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
示例 2
在同一个程序中,我们计算了包括年份和日期在内的两个时间段之间的差值。
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
使用 LocalDate 和 Period 类
LocalDate是用于表示日期的不可变日期时间对象。其默认格式为yyyy-MM-dd。它不能用于存储基于时间的值,它只描述日期。
Period类位于java.time 包中。它仅使用基于日期的值。
示例
在这个Java程序中,我们使用yyyy-MM-dd格式计算了两个时间段之间的差值。为此,我们使用了Period 类的内置方法between()。
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
结论
我们已经了解了Java中SimpleDateFormat、Date、LocalDate和Period类的用途,并且还了解了如何使用parse()和between()方法,这些方法在使用Java编程语言处理日期和时间时非常重要且有用。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP