localdate localdatetime
时间: 2023-09-09 21:09:33 浏览: 102
`LocalDate` and `LocalDateTime` are classes in the Java API that represent date and time values without considering time zones.
`LocalDate` represents a date (year, month, and day) without any specific time of day. It can be used to perform operations and calculations based on dates, such as checking if a date is before or after another, calculating the difference between two dates, or extracting specific components like the year or month.
Here's an example of using `LocalDate`:
```java
LocalDate currentDate = LocalDate.now();
System.out.println("Current date: " + currentDate);
LocalDate specificDate = LocalDate.of(2022, 7, 1);
System.out.println("Specific date: " + specificDate);
boolean isBefore = specificDate.isBefore(currentDate);
System.out.println("Is specific date before current date? " + isBefore);
```
`LocalDateTime` represents a date and time value without considering time zones. It includes the year, month, day, hour, minute, second, and nanosecond. It can be useful for scenarios where you need to work with both date and time information.
Here's an example of using `LocalDateTime`:
```java
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("Current date and time: " + currentDateTime);
LocalDateTime specificDateTime = LocalDateTime.of(2022, 7, 1, 12, 0);
System.out.println("Specific date and time: " + specificDateTime);
int hour = specificDateTime.getHour();
System.out.println("Hour of specific date and time: " + hour);
```
Both `LocalDate` and `LocalDateTime` are part of the `java.time` package introduced in Java 8. They provide a rich set of methods for manipulating and formatting date and time values in a localized manner.
阅读全文