What pattern of datetimeformat is needed for DateTimeFormatter to parse JAN01/2020? [duplicate]

不羁的心 提交于 2021-01-05 06:38:13

问题


I'm being passed a date in ths format - "JAN01/2020" but I can't seem to find the DateTimeFormatter pattern for it. I tried these (as well as several others) but they're resulting in DateTimeParseException -

 DateTimeFormatter.ofPattern("MMMd/YYYY").parse("JAN01/2020")
 DateTimeFormatter.ofPattern("MMMdd/YYYY").parse("JAN01/2020")

I also considered this post's solution and the three lines below also result in DateTimeParseException. It does not appear to be a case-sensitivity issue Java 8 DateTimeFormatter for month in all CAPS not working

DateTimeFormatter formatter= DateTimeFormatter.ofPattern("MMMdd/yyyy");
LocalDateTime dateTime = LocalDateTime.parse("JAN14/2020", formatter);
System.out.println(dateTime.getYear());

I appreciate any suggestions!


回答1:


You may

  • handle the case with use of DateTimeFormatterBuilder

    • .parseCaseInsensitive() for the case of the month, MMM is for Jan and you have JAN
    • .appendPattern("MMMdd/yyyy") for the pattern
  • parse into a LocalDate as there is no time composant

DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("MMMdd/yyyy");
DateTimeFormatter dateFormat = builder.toFormatter();

LocalDate dateTime = LocalDate.parse("JAN14/2020", dateFormat);
System.out.println(dateTime); // 2020-01-14

CODE DEMO




回答2:


Case-sensitive

The main problem with your format is, you are using YYYY instead of yyyy. Do it as follows:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;

class Main {
    public static void main(String args[]) {
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                                            .parseCaseInsensitive()
                                            .appendPattern("MMMd/yyyy")
                                            .toFormatter();
        System.out.println(LocalDate.parse("JAN01/2020", formatter));
    }
}

Output:

2020-01-01

Demo of using DateTimeFormatter::parse with it:

import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;

class Main {
    public static void main(String args[]) {
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .appendPattern("MMMd/yyyy")
                .toFormatter();
        System.out.println(formatter.parse("JAN01/2020"));
    }
}

Output:

{},ISO resolved to 2020-01-01



回答3:


  1. As the question you are linking to says, you need to use a DateTimeFormatterBuilder for case insensitive parsing of your month abbreviation in upper case.
  2. Beware of the case of format pattern letters. yyyy and YYYY are different. See for example this question: Can't parse String to LocalDate (Java 8).
  3. Supply a locale for your formatter. If JAN is English, then supply an English-speaking locale. For example:

        new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .appendPattern("MMMdd/yyyy")
                .toFormatter(Locale.ENGLISH)
    
  4. As azro said, since you haven’t got a time of day, parse into a LocalDate, not a LocalDateTime (or specify a default hour of day on the builder to be able to parse into a LocalDateTime, but I don’t see the advantage).



来源:https://stackoverflow.com/questions/62074063/what-pattern-of-datetimeformat-is-needed-for-datetimeformatter-to-parse-jan01-20

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