0

I have a pdf file where I want to know if the next line is a date, or just a string (there are two types of formats of the listing, and knowing if I've arrived at a date is important.) The trouble is, there appears to be no way to use date formatting to arrive at a date of 01 Apr 2020

LocalDate date = parseDate( "dd MMM yyyy", "01 Apr 2020" );

Throws ... Text '01 Apr 2020' could not be parsed at index 3

    private static LocalDate parseDate( final String format, final String s ) {

        final DateTimeFormatter df = DateTimeFormatter.ofPattern( format );
        LocalDate ld;   // Check if this was a legal LocalDate.
        try {
            ld = LocalDate.parse(s, df);
        } catch (java.time.format.DateTimeParseException pe) {
            System.out.println( pe.getMessage() );
            ld = null;  // This will signal an error
        }
        return ld;
    }

Is there really no way to parse that format of date, like a bank uses in their pdf?

Lotsa
  • 412
  • 4
  • 11
  • 2
    Are you sure of the `Locale` you use? – Boris Strandjev May 19 '20 at 05:55
  • Is that how to get that format? Use a different Locale? What Locale would I have to switch to in order to get that pattern to work? That's just how the bank sends the pdf. I need to know how to parse that format of date. There seems to be no way. – Lotsa May 19 '20 at 05:58
  • show the code for your `parseDate` method – Scary Wombat May 19 '20 at 06:00
  • 1
    If the parser uses Locale different than a type of English, you will not be able to parse the `Apr` part – Boris Strandjev May 19 '20 at 06:00
  • 1
    So try using this method https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ofPattern-java.lang.String-java.util.Locale- instead the one I think you use (that does not have `Locale` as second parameter. – Boris Strandjev May 19 '20 at 06:03
  • @Losta kindly show the code of `parseDate()`. – Deep Dalsania May 19 '20 at 06:04
  • I edited and showed parseDate – Lotsa May 19 '20 at 06:04
  • Yes, it was the Locale ... needed Locale.US Thanks for the quick help everyone! – Lotsa May 19 '20 at 06:14
  • 1
    It's better to use `Locale.ROOT`, as it's regarded the neutral locale. – MC Emperor May 19 '20 at 06:15
  • @MCEmperor In my opinion that depends. If the string is in English because English is the language used by computers across the world, I agree with you. If it’s in English because it was written specifically for a US audience, I consider `Locale.US` the correct choice. – Ole V.V. May 19 '20 at 06:38
  • 1
    I'm Canadian. Weird that my bank sends me an American format. Messes my code up. So, I now have Locale included in my parseDate method, and added a polymorph method that calls the new parseDate and passes in my own Locale (so my other calls still work.) Locale.US, or Local.Root is now the debate. – Lotsa May 19 '20 at 06:46
  • 2
    @OleV.V. I would make the assumption that a user wants a locale as neutral as possible, unless the user specifically supplies a locale-sensitive context. – MC Emperor May 19 '20 at 07:38

2 Answers2

5

Replace

final DateTimeFormatter df = DateTimeFormatter.ofPattern( format );

With

final DateTimeFormatter df = DateTimeFormatter.ofPattern( format , Locale.US );

Hopefully, this will resolve your issue.

Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
  • Thanks, that worked. However, now my parseDate method will need to be watched to see if the house burns down. I'll actually be making another method for this pdf read. Thanks for digging up what Locale to use. – Lotsa May 19 '20 at 06:13
-1

I believe you're using java8, You can do

LocalDate date = LocalDate.parse("01 Apr 2020", DateTimeFormatter.ofPattern("dd MMM yyyy", Locale.ROOT));

Edited: After pointed out that it doesn't work for all locales. Locale.ROOT should be used for neutral locale.

Vikash Tiwari
  • 113
  • 1
  • 10
  • 1
    Well, that's what I was doing. I just showed my call to my own parseDate method, but it calls the line you just showed, and it rejects that format. – Lotsa May 19 '20 at 06:02
  • you sure? Check it our here http://tpcg.io/CbYDYC5p – Vikash Tiwari May 19 '20 at 06:30
  • 1
    You must be in a Locale that uses that as a standard. My Locale doesn't accept it. Passing Locale.US worked, but I prefer the tip to use Locale.Root ... which is what I'll use from now on when my local formats get cranky. Locale.Root By the way that wasn't my downvote, thanks for helping. – Lotsa May 19 '20 at 06:39
  • Yeah, that's probably good. – Vikash Tiwari May 19 '20 at 06:43