Java unparsable date SimpleDateFormat [duplicate]

那年仲夏 提交于 2021-02-16 17:59:08

问题


I have a date that looks like that:

Sun Dec 29 00:24:09 CET 2019

I have a little utility method that parses a string date from a format to another:

public String formatDate(String date, String fromFormat, String toFormat) throws Exception {
        SimpleDateFormat from = new SimpleDateFormat(fromFormat);
        SimpleDateFormat to = new SimpleDateFormat(toFormat);
        return to.format(from.parse(date));
    }

However, with above date format, I do not find the correct date pattern to indicate to my method. According to SimpleDateFormat patterns documentation, it should be (if I am not mistaken), the following (for Sun Dec 29 00:24:09 CET 2019):

"E M d HH:mm:ss z yyyy"

However, it throws the following exception:

java.text.ParseException: Unparseable date: "Sun Dec 29 00:24:09 CET 2019"
        at java.text.DateFormat.parse(DateFormat.java:366)
        at com.aptar.simulator.Utils.formatDate(Utils.java:60)

The method is called like this:

formatDate(exDate, "E M d HH:mm:ss z yyyy", "dd-MM-yyyy HH:mm:ss");

Where

exDate = "Sun Dec 29 00:24:09 CET 2019"

回答1:


Date format should be

EEE MMM dd HH:mm:ss z yyyy

Your code works fine using this format.

using java.time API

LocalDate.parse(datestr, DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy")).format("TO DATE PATTERN");

Further details at Using java.time package to format date




回答2:


Try below solution -

formatDate("Sun Dec 29 00:24:09 CET 2019","EEE MMM d HH:mm:ss z yyyy","dd-MM-yyyy HH:mm:ss");

Format should be - "EEE MMM d HH:mm:ss z yyyy"

You should use EEE for Sun and MMM for Dec

hope this helps.




回答3:


Please find the code snippet below to solve your problem. The issue was the letter codes were correct, but there was character count mismatch , hence causing the issue. E.g.:Sun has three chars, but you were using a single E in your formatter.

public class Examp167 {
    public static String formatDate(String date, String fromFormat, String toFormat) throws Exception {
        SimpleDateFormat from = new SimpleDateFormat(fromFormat);
        SimpleDateFormat to = new SimpleDateFormat(toFormat);
        return to.format(from.parse(date));
    }
    public static void main(String[] args) throws Exception{
        String exDate  = "Sun Dec 29 00:24:09 CET 2019";
       System.out.println( formatDate(exDate, "EEE MMM dd HH:mm:ss zzz yyyy", "dd-MM-yyyy HH:mm:ss"));
    }
} 



回答4:


Firs use DateTimeFormatter instead of an old outdated class, then you should set the Locale since the day and month names are in English and last the in format needs to be MMM instead of M for the month

public static String formatDate(String date, String fromFormat, String toFormat) throws Exception {
    DateTimeFormatter inFormatter = DateTimeFormatter.ofPattern(fromFormat, Locale.US);

    DateTimeFormatter outFormatter = DateTimeFormatter.ofPattern(toFormat, Locale.US);
    return outFormatter.format(inFormatter.parse(date));
}

Example:

String exDate = "Sun Dec 29 00:24:09 CET 2019";
String out = formatDate(exDate, "E MMM d HH:mm:ss z yyyy", "dd-MM-yyyy HH:mm:ss");
System.out.println(out);

29-12-2019 00:24:09



来源:https://stackoverflow.com/questions/60000869/java-unparsable-date-simpledateformat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!