首页 > 解决方案 > SimpleDateFormat: Unparseable date when I try parse month: MMM dd, yyyy

问题描述

I try to parse string do the Date format, but get Unparserable exception:

    public void roundTest() throws ParseException {
        String pattern = "MMM dd, yyyy";
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String from = "Jul 01, 2021";
        Date result = sdf.parse(from);
    }

The same for java.time package:

    public void roundTest() throws ParseException {
        String from = "Jul 01, 2021";
        String pattern = "MMM dd, yyyy";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        System.out.println(LocalDate.parse(from, formatter));
    }

Error: "java.time.format.DateTimeParseException: Text 'Jul 01, 2021' could not be parsed at index 0"

Where the error?

标签: javadate

解决方案


推荐阅读