Home > Mobile >  Can't get LocalDate to Parse correctly
Can't get LocalDate to Parse correctly

Time:02-04

String date = "08/02/2022 Tuesday";
DateTimeFormatter LONG_DATE_FORMAT_ddMMyyyyEEEE = ofPattern("dd/MM/yyyy EEEE");
LocalDate.parse(date, LONG_DATE_FORMAT_ddMMyyyyEEEE);

I'm getting a DateTimeParseException with the following message: Text 08/02/2022 Tuesday' could not be parsed at index 11.

I suppose this is an issue with the EEEE side of my format, but I can't seem to understand what should replace it.

This is java 1.8.0_311

CodePudding user response:

LocalDate contains of a day, month, and year (Variation between 999999999-12-31 and -999999999-12-31)

Things like time and other values are rejected by the parsing. If you would like the day of the week, you can use a function like:

    // Parses the date
    LocalDate dt = LocalDate.parse("2018-11-27");

    // Prints the day
    System.out.println(dt.getDayOfWeek());

CodePudding user response:

This works for me:

String date = "08/02/2022 Tuesday";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy EEEE");
    
LocalDate time = LocalDate.parse(date, formatter);
System.out.println(time.format(formatter));
  •  Tags:  
  • Related