转换日期格式:Java 中的转换错误?
Converting date format: conversion error in Java?
我正在尝试将此日期转换为其他格式。不幸的是,未能成功解析日期并正确保留所有信息。
2017 年 12 月 6 日 07:14:56.656PM
至
2017-12-06 19:14:56.656
如果我尝试解析输入日期
LocalDateTime.parse("06-Dec-2017 07:14:56.656PM",
DateTimeFormatter.ofPattern("D-MMM-yyyy HH:mm:ss.SSSa"));
我收到以下错误 - 不确定这是什么意思?
Exception in thread "main" java.time.format.DateTimeParseException: Text '06-Dec-2017 07:14:56.656PM' could not be parsed: Conflict found: Field MonthOfYear 1 differs from MonthOfYear 12 derived from 2017-01-06
at java.time.format.DateTimeFormatter.createError(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalDateTime.parse(Unknown Source)
at com.cordys.coe.alf.logger.DBLogger.main(DBLogger.java:366)
Caused by: java.time.DateTimeException: Conflict found: Field MonthOfYear 1 differs from MonthOfYear 12 derived from 2017-01-06
at java.time.format.Parsed.crossCheck(Unknown Source)
at java.time.format.Parsed.crossCheck(Unknown Source)
如果尝试
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")
.format((new SimpleDateFormat("DD-MMM-yyyy HH:mm:ss.SSSa")
.parse("06-Dec-2017 07:14:56.656PM"))));
它给出的以下内容令人困惑,而且可能不正确。
2017-01-06 07:14:56:656
您有 2 个问题
- D应该是d
D
represents day of year
d
represents day-of-month
- HH 应该是 hh
H
represents hour-of-day (0-23)
h
represents clock-hour-of-am-pm (1-12)
请将D-MMM-yyyy HH:mm:ss.SSSa
改为d-MMM-yyyy hh:mm:ss.SSSa
有关详细信息,请查看 oracle docs
我正在尝试将此日期转换为其他格式。不幸的是,未能成功解析日期并正确保留所有信息。
2017 年 12 月 6 日 07:14:56.656PM 至 2017-12-06 19:14:56.656
如果我尝试解析输入日期
LocalDateTime.parse("06-Dec-2017 07:14:56.656PM",
DateTimeFormatter.ofPattern("D-MMM-yyyy HH:mm:ss.SSSa"));
我收到以下错误 - 不确定这是什么意思?
Exception in thread "main" java.time.format.DateTimeParseException: Text '06-Dec-2017 07:14:56.656PM' could not be parsed: Conflict found: Field MonthOfYear 1 differs from MonthOfYear 12 derived from 2017-01-06
at java.time.format.DateTimeFormatter.createError(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalDateTime.parse(Unknown Source)
at com.cordys.coe.alf.logger.DBLogger.main(DBLogger.java:366)
Caused by: java.time.DateTimeException: Conflict found: Field MonthOfYear 1 differs from MonthOfYear 12 derived from 2017-01-06
at java.time.format.Parsed.crossCheck(Unknown Source)
at java.time.format.Parsed.crossCheck(Unknown Source)
如果尝试
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")
.format((new SimpleDateFormat("DD-MMM-yyyy HH:mm:ss.SSSa")
.parse("06-Dec-2017 07:14:56.656PM"))));
它给出的以下内容令人困惑,而且可能不正确。 2017-01-06 07:14:56:656
您有 2 个问题
- D应该是d
D
represents day of year
d
represents day-of-month
- HH 应该是 hh
H
represents hour-of-day (0-23)
h
represents clock-hour-of-am-pm (1-12)
请将D-MMM-yyyy HH:mm:ss.SSSa
改为d-MMM-yyyy hh:mm:ss.SSSa
有关详细信息,请查看 oracle docs