LocalDateTime转String
LocalDateTime转String
package;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Test {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String timeStr = formatter.format(now);
System.out.println("timeStr = " + timeStr);
}
}
输出结果:
timeStr = 2022-08-15 17:09:08
String转LocalDateTime
package;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Test {
public static void main(String[] args) {
String timeStr = "2022-08-15 17:09:08";
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDate date = LocalDate.parse(timeStr, formatter);
LocalDateTime time = LocalDateTime.parse(timeStr, formatter);
System.out.println("date = " + date);
System.out.println("time = " + time);
}
}
输出结果:
date = 2022-08-15
time = 2022-08-15T17:09:08