将String转换为LocalDateTime:java.time.format.DateTimeParseException

26 浏览
0 Comments

将String转换为LocalDateTime:java.time.format.DateTimeParseException

这个问题已经有了答案:

如何使用LocalDateTime(Java 8)解析/格式化日期?

我正在使用以下代码将String中的日期转换为:

    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\'中的格式将保持不变,无法进行修改,因为我将在另一个应用程序中获取该变量。

admin 更改状态以发布 2023年5月23日
0
0 Comments

您输入字符串的日期格式应为: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);
}

0