简体   繁体   English

转换日期格式:Java中的转换错误?

[英]Converting date format: conversion error in Java?

I am trying to convert this date to a different format. 我正在尝试将此日期转换为其他格式。 unfortuantely havnt been successful parsing the date and retaining all the information correctly. 不幸的是,它无法成功解析日期并正确保留所有信息。

06-Dec-2017 07:14:56.656PM to 2017-12-06 19:14:56.656 2017年06月12日07:14:56.656PM至2017-12-06 19:14:56.656

If I try to parse the input date 如果我尝试解析输入日期

LocalDateTime.parse("06-Dec-2017 07:14:56.656PM",
        DateTimeFormatter.ofPattern("D-MMM-yyyy HH:mm:ss.SSSa"));

I get the following error - not sure what it means? 我收到以下错误-不确定是什么意思?

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)

and if try 如果尝试

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"))));

It give the following which is confusing, and probably not correct. 它给出了以下令人困惑的信息,并且可能不正确。 2017-01-06 07:14:56:656 2017-01-06 07:14:56:656

You have 2 problems 你有两个问题

  1. D should be d D应该是d

D represents day of year D代表一年中的某天

d represents day-of-month d代表月中的某天

  1. HH should be hh HH应该是hh

H represents hour-of-day (0-23) H代表小时(0-23)

h represents clock-hour-of-am-pm (1-12) h表示上午的时钟小时(1-12)

Please change D-MMM-yyyy HH:mm:ss.SSSa to d-MMM-yyyy hh:mm:ss.SSSa 请将D-MMM-yyyy HH:mm:ss.SSSad-MMM-yyyy hh:mm:ss.SSSa

For more information , check the oracle docs 有关更多信息,请检查oracle docs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM