Java中的LocalDateTime及其示例

时间:2020-01-09 10:35:17  来源:igfitidea点击:

java.time.LocalDateTime类是Java 8中添加的新日期和时间API的一部分,该API代表ISO-8601日历系统中的日期时间,例如2019-10-03T11:15:35,它表示日期-时间,通常被视为年-月-日-时-分-秒。 LocalDateTime类不存储或者表示时区。相反,它是对用于生日的日期的描述,以及在墙上时钟上看到的本地时间。

LocalDateTime类是不可变的,因此是线程安全的。由于已将其标记为最终的,因此无法进行扩展。在这篇文章中,我们将看到一些示例,这些示例演示Java LocalDateTime类的用法。

创建LocalDateTime的实例

Java中的LocalDateTime类没有任何公共构造函数来获取实例,我们将使用工厂方法来获取实例。

1.使用now()方法,我们可以从系统时钟的默认时区中获取当前日期时间。

LocalDateTime dateTime = LocalDateTime.now();
System.out.println(dateTime); //2019-10-30T10:29:37.082914100

2.使用of()方法,我们可以通过传递年,月,日和小时,分钟,秒值来创建LocalDateTime的实例。

还有一个of()方法可通过传递LocalDate和LocalTime实例来创建LocalDateTime的实例。

  • 的(LocalDate日期,LocalTime时间)
LocalDateTime dateTime = LocalDateTime.of(2019, 10, 28, 11, 59, 59);
System.out.println(dateTime); //2019-10-28T11:59:59

特定时区的LocalDateTime

我们还可以通过传递时区ID从指定时区的系统时钟获取当前日期时间。

ZoneId zone1 = ZoneId.of("America/Los_Angeles");
ZoneId zone2 = ZoneId.of("Asia/Kolkata");
LocalDateTime ldt1 = LocalDateTime.now(zone1);
LocalDateTime ldt2 = LocalDateTime.now(zone2);

System.out.println(ldt1); //2019-10-29T22:05:57.729368200
System.out.println(ldt2); //2019-10-30T10:35:57.827541900

从LocalDateTime获取日期和时间值

由于LocalDateTime同时具有日期和时间值,因此它也具有获取年,月,日值的方法以及获取时,分,秒值的方法。

public class FormatDate {
  public static void main(String[] args) {
    LocalDateTime dateTime = LocalDateTime.of(2019, 10, 28, 11, 59, 59);
    System.out.println("Date-Time: " + dateTime);
    
    System.out.println("Year- " + dateTime.getYear());
    System.out.println("Month- " + dateTime.getMonthValue());
    System.out.println("Day- " + dateTime.getDayOfMonth());
    
    System.out.println("Hour- " + dateTime.getHour());
    System.out.println("Minute- " + dateTime.getMinute());
    System.out.println("Second- " + dateTime.getSecond());
  }
}

输出:

Date-Time: 2019-10-28T11:59:59
Year- 2019
Month- 10
Day- 28
Hour- 11
Minute- 59
Second- 59

格式化LocalDateTime

使用DateTimeFormatter,我们可以指定用于格式化LocalDateTime的模式。

public class FormatDate {
  public static void main(String[] args) {
    // get datetime
    LocalDateTime dateTime = LocalDateTime.now();
    // Format pattern
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
    System.out.println(dateTime.format(formatter));
    // Format pattern
    formatter = DateTimeFormatter.ofPattern("cccc, MMM dd, yyyy hh:mm:ss a");
    System.out.println(dateTime.format(formatter));
  }
}

输出:

2019-10-30T11:06:51.899
Wednesday, Oct 30, 2019 11:06:51 AM

使用LocalDateTime进行日期计算

有一些方法可以从LocalDateTime添加或者减去天,月和年。

public class FormatDate {
  public static void main(String[] args) {
    // get datetime
    LocalDateTime dateTime = LocalDateTime.now();

    System.out.println("Created Date-Time: " + dateTime);  
    System.out.println("Date after subtraction - " + dateTime.minusDays(40));
    System.out.println("Date after year subtraction - " + dateTime.minusYears(2));
     
    System.out.println("Date after addition - " + dateTime.plusDays(40));
    System.out.println("Date after year addition - " + dateTime.plusYears(2));
  }
}

输出:

Created Date-Time: 2019-10-30T11:11:06.820635900
Date after subtraction - 2019-09-20T11:11:06.820635900
Date after year subtraction - 2017-10-30T11:11:06.820635900
Date after addition - 2019-12-09T11:11:06.820635900
Date after year addition – 2021-10-30T11:11:06.820635900

使用LocalDateTime进行时间计算

有一些方法可以从LocalDateTime添加或者减去小时,分钟,秒,纳秒。

public class FormatDate {
  public static void main(String[] args) {
    // get datetime
    LocalDateTime dateTime = LocalDateTime.now();

    System.out.println("Created Date-Time: " + dateTime);  

    System.out.println("Hour after subtraction- " + dateTime.minusHours(2));
    System.out.println("Minute after subtraction- " + dateTime.minusMinutes(10));
    System.out.println("Second after subtraction- " + dateTime.minusSeconds(20));
    System.out.println("NanoSecond after subtraction- " + dateTime.minusNanos(100));

    System.out.println("Hour after addition- " + dateTime.plusHours(1));
    System.out.println("Minute after addition- " + dateTime.plusMinutes(15));
    System.out.println("Second after addition- " + dateTime.plusSeconds(25));
    System.out.println("NanoSecond after addition- " + dateTime.plusNanos(100));
  }
}

输出:

Created Date-Time: 2019-10-30T11:14:07.632356
Hour after subtraction- 2019-10-30T09:14:07.632356
Minute after subtraction- 2019-10-30T11:04:07.632356
Second after subtraction- 2019-10-30T11:13:47.632356
NanoSecond after subtraction- 2019-10-30T11:14:07.632355900
Hour after addition- 2019-10-30T12:14:07.632356
Minute after addition- 2019-10-30T11:29:07.632356
Second after addition- 2019-10-30T11:14:32.632356
NanoSecond after addition- 2019-10-30T11:14:07.632356100

在Java中比较LocalDateTimes

为了比较两个LocalDateTime实例,有以下方法:

  • compareTo(ChronoLocalDateTime <?other)-比较此日期时间与另一个日期时间。如果小于传递的LocalDateTime实例,则返回负值;如果大于,则返回正值。

  • isAfter(ChronoLocalDateTime <?other)-检查此日期时间是否在指定的日期时间之后。

  • isBefore(ChronoLocalDateTime <?other)-检查此日期时间是否早于指定的日期时间。

  • isEqual(ChronoLocalDateTime <?other)-检查此日期时间是否等于指定的日期时间。

public class FormatDate {
  public static void main(String[] args) {
    // get datetime
    LocalDateTime ldt1 = LocalDateTime.of(2019, Month.OCTOBER, 25, 20, 25, 45);
    LocalDateTime ldt2 = LocalDateTime.of(2019, Month.SEPTEMBER, 20, 22, 18, 40);

    System.out.println("Created Date-Time1: " + ldt1);  
    System.out.println("Created Date-Time2: " + ldt2);

    System.out.println(ldt1.compareTo(ldt2));
    System.out.println(ldt2.compareTo(ldt1));

    System.out.println(ldt1.isAfter(ldt2));
    System.out.println(ldt1.isBefore(ldt2));
    System.out.println(ldt1.isEqual(ldt2));
  }
}

输出:

Created Date-Time1: 2019-10-25T20:25:45
Created Date-Time2: 2019-09-20T22:18:40
1
-1
true
false
false