LocalDateTime
时间: 2023-10-14 13:24:11 浏览: 39
LocalDateTime is a class in Java 8 that represents a date and time without a time zone. It stores the date and time values in the ISO-8601 format and provides various methods to manipulate them. The class is immutable, which means that the values cannot be changed once they are set.
LocalDateTime is useful for representing events that occur in a specific location or for performing date and time calculations that are not dependent on time zones. For example, you can use LocalDateTime to represent the date and time of a user's activity on a website, or to calculate the duration between two dates and times.
To create a LocalDateTime object, you can use the following code:
```java
LocalDateTime now = LocalDateTime.now();
```
This creates a LocalDateTime object with the current date and time. You can also create a LocalDateTime object with specific values using the `of` method:
```java
LocalDateTime dateTime = LocalDateTime.of(2022, Month.JANUARY, 1, 0, 0, 0);
```
This creates a LocalDateTime object for January 1st, 2022 at midnight.
Once you have a LocalDateTime object, you can use various methods to manipulate the values. For example, you can add or subtract days, hours, minutes, and seconds:
```java
LocalDateTime newDateTime = dateTime.plusDays(7).minusHours(3);
```
This creates a new LocalDateTime object that is 7 days later than the original object and 3 hours earlier.
Overall, LocalDateTime provides a flexible and powerful way to work with dates and times in Java without worrying about time zones.