在 java 中,如果我有一个如下所示的字符串:“Nov 30, 2016”(这是 FormatStyle.MEDIUM),如何将其转换为 LocalDateTime 数据类型?
最佳答案
我在 JDK 8 上运行了这个:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class DateFormatDemo {
public static void main(String[] args) {
String dateStr = "Nov 30, 2016";
LocalDateTime ldt = LocalDateTime.parse(dateStr, DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
System.out.println("date string: " + dateStr);
System.out.println("local date : " + ldt);
}
}
这是我的输出:
date string: Nov 30, 2016
local date : 2016-11-30
Process finished with exit code 0
我在使用 LocalDateTime
时遇到了原始代码异常,因为输入 String
不包含时间。
如果您想要 LocalDateTime
,请尝试在输入中添加 hh:mm:ss
。
关于java 如何将 FormatStyle.MEDIUM ("Nov 30, 2016") 字符串转换为 LocalDateTime 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40896138/