Java 程序计算两个时间段之间的差异

javaobject oriented programmingprogramming更新于 2024/5/7 9:16:17

要在 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。我们为 SimpleDateFormat 类创建了一个名为 timeformat 的对象。为了按照给定的格式将文本从字符串转换为日期,我们使用了 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 编程语言处理日期和时间时非常重要且有用。


相关文章


有用资源