0

I want the currentDate in the format MMMM d, YYYY

LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy");
String createdOnDate = alertspage.createdondate().trim();
LocalDate Date = LocalDate.parse(currentDate, formatter);
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 3
    `currentDate` is a `LocalDate` - that does not have a format - it only has a default format used when it is converted to `String` using its `toString()` method - you need to use the `DateTimeFormatter` to `format` the `LocalDate` as required – user16320675 Jun 26 '23 at 12:25

1 Answers1

0

tl;dr

the today's date in the desired format

System.out.println(
    LocalDate.now().format(
        DateTimeFormatter.ofPattern(
            "MMMM, d uuuu",
            Locale.ENGLISH
        )
    )
);

Explanation

You're attempt to retrieve the currentDate in the format MMMM d, YYYY does not work because you don't produce the String output.
Instead, you try to parse(currentDate, formatter), wich cannot won't be working either due to an unexpected type of the first parameter/argument (expects a String, you pass a LocalDate), or — if LocalDate.toString() is used implicitly — it won't have your custom format.

Demo

Code

public static void main(String[] args) {
    // get today's date as LocalDate
    LocalDate today = LocalDate.now();
    // provide a DateTimeFormatter for the desired OUTPUT
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
                                        "MMMM d, uuuu",
                                        Locale.ENGLISH
                                  );
    // print the resulting LocalDate's toString() (implicitly)
    System.out.println("toString(): " + today);
    // print the resulting LocalDate formatted by the formatter
    System.out.println("formatted:  " + today.format(formatter));
}

Output

toString(): 2023-06-26
formatted:  June 26, 2023
deHaar
  • 17,687
  • 10
  • 38
  • 51