2

I use the following code:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MMM dd");
LocalDate date = LocalDate.parse(s, formatter);

to parse date strings, e.g. "2019 Feb 25". But I also want this code to parse strings like this: "2019 February 25". Can I somehow change the format string "yyyy MMM dd" to achieve this?

I tried the following format strings "yyyy MMMM dd", "yyyy MMM[M] dd", "yyyy [M+] dd", but I can't make it parse both "2019 Feb 25" and "2019 February 25".

Daniel
  • 295
  • 2
  • 9
  • 2
    February has 28 days in common years or 29 in leap year, Not 30 – Youcef LAIDANI Aug 08 '20 at 14:01
  • 6
    Does this answer your question? [DateTimeFormatter Accepting Multiple Dates and Converting to One (java.time library)](https://stackoverflow.com/questions/44600420/datetimeformatter-accepting-multiple-dates-and-converting-to-one-java-time-libr) – adq Aug 08 '20 at 14:03
  • @YCF_L I changed the day to 25 from 30 – Daniel Aug 08 '20 at 14:09

2 Answers2

2

The following code snippet based on the suggested answer proves that it is possible to parse differently formatted dates using one parser.

However, such parser should not be used for output because it prints using multiple formats.

    // using several formats in parser
    DateTimeFormatter parser = DateTimeFormatter.ofPattern(
        "[yyyy [MMMM][MMM] dd][yyyy-MM-dd][MM/dd/yyyy]");

    // prepare single format for output
    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");


    LocalDate d1 = LocalDate.parse ("2019 Feb 31", parser); // parsed correctly to the last Feb day
    LocalDate d2 = LocalDate.parse ("2019 February 26", parser);
    LocalDate d3 = LocalDate.parse ("2019-02-27", parser);
    LocalDate d4 = LocalDate.parse("02/30/2020", parser); // parsed to the last Feb date in a leap year

    System.out.println (d1.format (parser));
      
    System.out.println (d1.format (format));
    System.out.println (d2.format (format));
    System.out.println (d3.format (format));
    System.out.println (d4.format (format));

Output

2019 FebruaryFeb 282019-02-2802/28/2019                                                                                                        
2019-02-28                                                                                                                                             
2019-02-26                                                                                                                                             
2019-02-27                                                                                                                                             
2020-02-29 

The first line of the output is messed up by the parser format.

Online demo at GDB

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
2

If you specify the pattern "yyyy [MMMM][MMM] dd" then it works:

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

public class Test {
    public static void main(String[] args) {
        String[] dates = new String[]{"2019 January 25", "2019 February 25", "2019 March 25", "2019 April 25",
                "2019 May 25", "2019 June 25", "2019 July 25", "2019 August 25", "2019 September 25", "2019 October 25",
                "2019 November 25", "2019 December 25", "2019 Jan 25", "2019 Feb 25", "2019 Mar 25", "2019 Apr 25",
                "2019 May 25", "2019 Jun 25", "2019 Jul 25", "2019 Aug 25", "2019 Sep 25", "2019 Oct 25", "2019 Nov 25",
                "2019 Dec 25"
        };

        for (String date : dates) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy [MMMM][MMM] dd");
            LocalDate dateMMM = LocalDate.parse(date, formatter);
            System.out.println(dateMMM);
        }
    }
}