1

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!

Moshe
  • 65
  • 6

3 Answers3

4

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
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Trying to understand this, why doesn't this work with 'y' lowercase or uppercase- DateTimeFormatter.ofPattern("MMMd/yyyy").parse and DateTimeFormatter.ofPattern("MMMd/YYYY").parse are throwing the same exception – Moshe May 28 '20 at 21:02
4

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

azro
  • 53,056
  • 7
  • 34
  • 70
  • Trying to understand this, why doesn't this work with 'y' lowercase- DateTimeFormatter.ofPattern("MMMd/yyyy").parse("JAN01/2020") – Moshe May 28 '20 at 20:55
  • Uppercase or lowercase, both will not work? If it's because of case then wouldn't one be correct? – Moshe May 28 '20 at 21:00
  • What's the distinction here, this works fine with month total UPPERCASE - DateTimeFormatter.ofPattern("d-MMM-yyyy").parse("01-Aug-2019") – Moshe May 28 '20 at 21:15
  • 1
    @Moshe `Aug` is not total uppercase, total uppercase is `AUG` ;) it's about the value, not the pattern. Read my post again, i've explain where is the problem about the case – azro May 28 '20 at 21:16
  • @Moshe you may now think about accepting an answer, is one solves your problem ;) – azro May 31 '20 at 12:21
0
  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).

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161