datetimeformatter
时间: 2023-09-07 09:17:47 浏览: 100
DateTimeFormatter is a class in Java used to format and parse date and time values. It is part of the java.time package introduced in Java 8. It provides methods to format date and time values into strings and parse strings into date and time values.
The DateTimeFormatter class uses a pattern string to specify the format of the date and time values. The pattern string consists of letters and symbols that represent various parts of the date and time values, such as year, month, day, hour, minute, second, and time zone.
Example:
```
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
String formattedDateTime = now.format(formatter);
System.out.println(formattedDateTime); // output: 18/08/2021 14:30:45
```
In this example, we create a LocalDateTime object representing the current date and time. We then create a DateTimeFormatter object with a pattern string that specifies the format of the date and time values. We use the format() method of the LocalDateTime object to format the date and time values into a string using the specified formatter. Finally, we print the formatted date and time string to the console.
阅读全文