我使用下面的代码来转换字符串中的日期:
String strDate="Thu Aug 09 16:01:46 IST 2018";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
LocalDateTime dateTime = LocalDateTime.parse(strDate,formatter);
我收到以下异常:
java.time.format.DateTimeParseException: Text 'Thu Aug 09 16:01:46 IST 2018' could not be parsed at index 0
变量“strDate”中的格式将相同且无法修改,因为我将从不同的应用程序获取该格式
最佳答案
输入字符串的日期格式为:E MMM dd HH:mm:ss z yyyy
。下面的代码应该可以正常工作,没有任何错误
public static void main(String[] args) throws IOException {
String strDate = "Thu Aug 09 16:01:46 IST 2018";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy");
LocalDateTime dateTime = LocalDateTime.parse(strDate, formatter);
}
关于java - 字符串到 LocalDateTime 转换 : java. time.format.DateTimeParseException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51772327/