Java LocalDateTime to String Conversion: A Comprehensive Guide
You can use the DateTimeFormatter class to convert a LocalDateTime object to a string in Java. Here's an example:
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss');
String formattedDateTime = now.format(formatter);
System.out.println(formattedDateTime);
In this example, we obtain the current date and time using LocalDateTime.now(). Then, we create a formatter using DateTimeFormatter.ofPattern() to specify the desired string representation of the date and time. Finally, we use the format() method to convert the LocalDateTime object to a string and print it to the console.
The output will be similar to the following:
2021-07-07 12:30:45
原文地址: http://www.cveoy.top/t/topic/nOeP 著作权归作者所有。请勿转载和采集!