将string转为localDate
时间: 2024-01-14 17:03:30 浏览: 185
要将字符串转换为LocalDate
,你可以使用LocalDate.parse()
方法。该方法接受一个表示日期的字符串和日期格式作为参数,并返回对应的LocalDate
对象。以下是一个示例代码:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// 日期字符串
String dateString = "2022-01-15";
// 定义日期格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 将字符串转换为LocalDate
LocalDate date = LocalDate.parse(dateString, formatter);
// 输出转换后的LocalDate
System.out.println("转换后的日期: " + date);
}
}
在上面的示例中,我们将字符串"2022-01-15"转换为LocalDate
对象。我们使用DateTimeFormatter
指定了日期的格式为"yyyy-MM-dd"。然后,我们使用LocalDate.parse()
方法将字符串转换为LocalDate
对象,并将其存储在date
变量中。最后,我们输出转换后的LocalDate
对象。
相关推荐

















