Java中LocalDate、LocalTime和LocalDateTime类的常用方法


在Java环境中,与日期和时间相关的最重要的类有三种:LocalDate、LocalTime和LocalDateTime。Java编程语言提供这些类来处理与日期和时间相关的问题。我们需要导入Java包`java.time`,它是日期、时间、瞬间和时间持续时间的核心应用程序编程接口(API)。

LocalDate、LocalTime和LocalDateTime类的用途:

  • java.time.* //包含所有类

  • java.time.LocalDate //用于LocalDate

  • java.time.LocalDateTime //用于LocalDateTime

  • java.time.LocalTime //用于LocalTime

    • java.time.LocalDate - 此函数保存一个不含任何时区的日期。格式将符合ISO-8601标准,并采用默认格式。

    • java.time.LocalTime - 这是一个与ISO 8601日历系统协同工作的特定系统。我们将获得不含任何时区和日期的本地时间。

    • java.time.LocalDateTime - 此函数提供不含任何时区的本地时间和当前日期。在查找实际本地时间时,它还包含纳秒部分。

以下是一个示例:

localDate: 2022-04-11
localTime: 12:15:26.343
localDateTime: 2022-04-11T12:15:26.344

在LocalDate、LocalTime和LocalDateTime类中使用方法的算法

在这个算法中,我们将解释如何在Java环境中使用LocalDate、LocalTime和LocalDateTime类。通过使用这个算法,我们将构建一些Java语法,这将有助于我们进一步构建一些Java代码。

  • 步骤1 - 开始进程。

  • 步骤2 - 声明输入输出流。

  • 步骤3 - 从Java时间包中声明并导入LocalDate、LocalTime、LocalDateTime类。

  • 步骤4 - 定义一个主类。

  • 步骤5 - 定义参数字符串。

  • 步骤6 - 构造LocalDate类的实例。

  • 步骤7 - 使用now()方法。

  • 步骤8 - 显示当前日期。

  • 步骤9 - 构造LocalDateTime类的实例。

  • 步骤10 - 使用now()方法。

  • 步骤11 - 构造LocalTime类。

  • 步骤12 - 使用now()方法。

  • 步骤13 - 声明plusHours方法。

  • 步骤14 - 获取返回值。

  • 步骤15 - 终止进程。

在LocalDate、LocalTime和LocalDateTime类中使用方法的语法

LocalDate localDate = LocalDate.now();
System.out.println("localDate Today: "+localDate);
LocalTime localTime =LocalTime.now();
System.out.println("localTime Today: "+localTime);
LocalDateTime localDateTime =LocalDateTime.now();
System.out.println("localDateTime Today: "+localDateTime);
LocalDate localDate = LocalDate.of(2023,04,11);
System.out.println("localDate Today: "+localDate);
LocalTime localTime =LocalTime.of(23,12,56,234);
System.out.println("localTime Today: "+localTime);
LocalDateTime localDateTime =LocalDateTime.of(2023,12,01,23,12,56,234);
System.out.println("localDateTime Today: "+localDateTime);
LocalDate localDateNov = LocalDate.of(2023, Month. APRIL,11);
System.out.println("localDateApril: "+localDateNov);
System.out.println("Today: "+LocalDate.now());
LocalDate todayPlus10Days=LocalDate.now().plusDays(10);
System.out.println("localDatePlus10: "+todayPlus10Days);
System.out.println("Time Now: "+LocalTime.now());
LocalTime nowMinus20Minutes=LocalTime.now().minusMinutes(20);
System.out.println("nowMinus20Minutes: "+nowMinus20Minutes);
System.out.println("Date-Time Now: "+LocalDateTime.now());
LocalDateTime nowPlus2Years =LocalDateTime.now().plusYears(2);
System.out.println("todayPlus2Years: "+nowPlus2Years);
public static void main(String[] args) {
	LocalDate date = LocalDate.now();
	LocalDate yesterday = date.minusDays(1);
	LocalDate tomorrow = yesterday.plusDays(2);
	System.out.println("Today date: "+date);
	System.out.println("Yesterday date: "+yesterday);
	System.out.println("Tomorrow date: "+tomorrow);
}

在这个语法中,我们尝试向您展示如何使用LocalDate、LocalTime和LocalDateTime方法类来获取其实际值。通过使用上述语法,我们将构建一些Java代码来有效地解释问题陈述。

遵循的方法

  • 方法1 - Java程序演示LocalDate、LocalTime和LocalDateTime类中常用的方法

  • 方法2 - Java程序通过使用各种方法来说明LocalDate、LocalTime和LocalDateTime类中常用的方法

  • 方法3 - Java程序通过使用Instant和Temporal方法来说明LocalDate、LocalTime和LocalDateTime类中常用的方法

方法1:使用LocalDate、LocalTime和LocalDateTime类

在这种方法中,我们将尝试使用特定LocalDate类的now()方法。在这里,我们可以根据系统时钟和默认时区获取LocalDate的值。

示例

//Java Program to demonstrate the Commonly used methods in classes LocalDate, LocalTime and LocalDateTime Classes
import java.io.*;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
public class ARBRDD {
   public static void main (String[] args) {
      LocalDate presentDate = LocalDate.now();
      System.out.println(presentDate);
      LocalDateTime present = LocalDateTime.now();
      System.out.println(present);
      LocalTime presentTime = LocalTime.now();
      System.out.println(presentTime);
   }
}

输出

2023-04-11
2023-04-11T13:25:11.545488429
13:25:11.545575593

方法2:使用带有GetDay函数的LocalDate、LocalTime和LocalDateTime类

在这种方法中,我们将讨论一些与LocalDate、LocalDateTime、LocalTime类相关的实用程序方法。它们是:

  • getDayOfMonth() - 返回月份中的某一天

  • getDayOfWeek() - 返回星期几

  • getDayOfYear() - 返回一年中的某一天

  • getMonth() - 返回月份的名称

  • getMonthValue() - 返回月份的数值

示例

//Java Program to illustrate Commonly used methods in classes LocalDate, LocalTime and LocalDateTime Classes by using various methods
import java.io.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class ARBRDD {
   public static void main(String[] args){
      LocalDate presentDate = LocalDate.now();
      System.out.println(presentDate);
      LocalDateTime present = LocalDateTime.now();
      System.out.println(present);
      LocalTime presentTime = LocalTime.now();
      System.out.println(presentTime);
      System.out.println(presentDate.getDayOfMonth());
      System.out.println(presentDate.getDayOfWeek());
      System.out.println(presentDate.getDayOfYear());
      System.out.println(presentDate.getMonth());
      System.out.println(presentDate.getMonthValue());
      System.out.println(presentDate.isLeapYear());
      System.out.println(presentDate.lengthOfYear());
      System.out.println(presentDate.lengthOfMonth());
      System.out.println(presentDate.plusDays(50));
      System.out.println(presentDate.plusMonths(50));
      System.out.println(presentDate.plusYears(50));
      System.out.println(presentDate.minusDays(50));
      System.out.println(presentDate.minusMonths(50));
      System.out.println(presentDate.minusYears(50));
      System.out.println(present.plusHours(100));
      System.out.println(present.plusMinutes(1000));
      System.out.println(present.plusSeconds(100000));
      System.out.println(present.plusNanos(1000000));
   }
}

输出

2023-04-11
2023-04-11T13:29:55.830090
13:29:55.831305
11
TUESDAY101
APRIL
4
false
365
30
2023-05-31
2027-06-11
2073-04-11
2023-02-20
2019-02-11
1973-04-11
2023-04-15T17:29:55.830090
2023-04-12T06:09:55.830090
2023-04-12T17:16:35.830090
2023-04-11T13:29:55.831090

方法3:使用Instant和Temporal方法获取时间、日期和日期时间

Instant方法的使用

在这个Java示例中,我们尝试向您展示如何实现Instant类以及Temporal方法来获取本地日期和时间,而无需提及用户的当前时区。

示例

//Java Program to illustrate Commonly used methods in classes LocalDate, LocalTime and LocalDateTime Classes by using the instant method
package com.howtodoinjava.core.datetime;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
public class AddHoursMinutesSecondsToDate {
   public static void main(final String[] args) {
      //1 - Same methodss for ZonedDateTime and OffsetDateTime
      LocalDateTime now = LocalDateTime.now();
      System.out.println("Current Date and Time = " + now);
      LocalDateTime updatedTime = now.plusHours(2);
      System.out.println("Updated Date and Time = " + updatedTime);
      updatedTime = now.plusMinutes(20);
      System.out.println("Updated Date and Time = " + updatedTime);
      updatedTime = now.plus(Duration.ofMillis(8000));
      System.out.println("Updated Date and Time = " + updatedTime);
      updatedTime = now.plus(20, ChronoUnit.HOURS);
      System.out.println("Updated Date and Time = " + updatedTime);
       updatedTime = now.minusHours(2);
      updatedTime = now.minusMinutes(20);
      updatedTime = now.minusSeconds(300);
      updatedTime = now.minus(Duration.ofMillis(8000));
      updatedTime = now.minus(20, ChronoUnit.HOURS);
      Instant currentInstant = Instant.parse("2022-06-24T05:12:35Z");
      System.out.println("Current Date and Time = " + currentInstant);
      Instant updatedInstant = currentInstant.plus(2, ChronoUnit.HOURS);
      System.out.println("Updated Date and Time = " + updatedInstant);
      updatedInstant = currentInstant.plusSeconds(300);
      updatedInstant = currentInstant.plusMillis(8000);
      updatedInstant = currentInstant.plusNanos(600000);
      updatedInstant = currentInstant.plusSeconds(300);
      System.out.println("Updated Date and Time = " + updatedInstant);
      //2 java.util.Date
      Date date = new Date();
      Calendar cal = Calendar.getInstance();
      cal.setTime(date);
      System.out.println("Current Date and Time = " + cal.getTime());
      cal.add(Calendar.HOUR, 2);
      System.out.println("Updated Date and Time = " + cal.getTime());
      cal.add(Calendar.MINUTE, -15);
      System.out.println("Updated Date and Time = " + cal.getTime());
      cal.add(Calendar.SECOND, 10);
      System.out.println("Updated Date and Time = " + cal.getTime());
   }
}

输出

Current Date and Time = 2023-04-11T13:32:32.989642529
Updated Date and Time = 2023-04-11T15:32:32.989642529
Updated Date and Time = 2023-04-11T13:52:32.989642529
Updated Date and Time = 2023-04-11T13:32:40.989642529
Updated Date and Time = 2023-04-12T09:32:32.989642529
Current Date and Time = 2022-06-24T05:12:35Z
Updated Date and Time = 2022-06-24T07:12:35Z
Updated Date and Time = 2022-06-24T05:17:35Z
Current Date and Time = Tue Apr 11 13:32:32 GMT 2023
Updated Date and Time = Tue Apr 11 15:32:32 GMT 2023
Updated Date and Time = Tue Apr 11 15:17:32 GMT 2023
Updated Date and Time = Tue Apr 11 15:17:42 GMT 2023

Instant方法的使用

在这个Java示例中,我们尝试向您展示如何实现Temporal方法来获取本地日期和时间,而无需提及用户的当前时区。

示例

//Java Program to illustrate Commonly used methods in classes LocalDate,
LocalTime and LocalDateTime Classes by using the temporal method
package com.howtodoinjava.core.datetime;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class DaysBetweenDates {
   public static void main(final String[] args) {
      LocalDate date1 = LocalDate.now();
      LocalDate date2 = date1.plusDays(99);
      long diffInDays = ChronoUnit.DAYS.between(date1, date2);
      System.out.println(diffInDays);
      diffInDays = date1.until(date2, ChronoUnit.DAYS);
      System.out.println(diffInDays);
      LocalDate startDate = LocalDate.now();
      LocalDate endDate = startDate.plusMonths(2);
      long numOfDays = ChronoUnit.DAYS.between(startDate, endDate);
      List<LocalDate> listOfDates = Stream.iterate(startDate, date ->
      date.plusDays(1))
      .limit(numOfDays)
      .collect(Collectors.toList());
      System.out.println(listOfDates);
   }
}

输出

99
99
[2023-04-11, 2023-04-12, 2023-04-13, 2023-04-14, 2023-04-15, 2023-04-16, 2023-
04-17, 2023-04-18, 2023-04-19, 2023-04-20, 2023-04-21, 2023-04-22, 2023-04-23,
2023-04-24, 2023-04-25, 2023-04-26, 2023-04-27, 2023-04-28, 2023-04-29, 2023-
04-30, 2023-05-01, 2023-05-02, 2023-05-03, 2023-05-04, 2023-05-05, 2023-05-06,
2023-05-07, 2023-05-08, 2023-05-09, 2023-05-10, 2023-05-11, 2023-05-12, 2023-
05-13, 2023-05-14, 2023-05-15, 2023-05-16, 2023-05-17, 2023-05-18, 2023-05-19,
2023-05-20, 2023-05-21, 2023-05-22, 2023-05-23, 2023-05-24, 2023-05-25, 2023-
05-26, 2023-05-27, 2023-05-28, 2023-05-29, 2023-05-30, 2023-05-31, 2023-06-01,
31
2023-06-02, 2023-06-03, 2023-06-04, 2023-06-05, 2023-06-06, 2023-06-07, 2023-
06-08, 2023-06-09, 2023-06-10]

结论

java.time类是不可变类,它们以默认格式表示日期和时间的数值。在今天的这篇文章中,我们尝试使用算法构建一些Java语法,然后演示一些可能的Java代码来有效地解释问题陈述。

更新于:2023年12月27日

82 次浏览

开启您的职业生涯

完成课程获得认证

开始学习
广告