yyyy-MM-dd格式的字符串转化成LocalDateTime
时间: 2024-04-29 15:17:51 浏览: 19
要将 "yyyy-MM-dd" 格式的字符串转换为 LocalDateTime,你可以使用 Java 8 中的 `DateTimeFormatter` 和 `LocalDate` 类。首先,你需要创建一个 `DateTimeFormatter` 对象,用于指定日期格式。然后,使用 `LocalDate.parse()` 方法将字符串解析为 `LocalDate` 对象。最后,你可以使用 `atStartOfDay()` 方法将 `LocalDate` 对象转换为 `LocalDateTime` 对象。
以下是一个示例:
```java
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String dateString = "2022-01-01";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.parse(dateString, formatter);
LocalDateTime localDateTime = localDate.atStartOfDay();
System.out.println(localDateTime);
}
}
```
这段代码将输出:
```
2022-01-01T00:00
```