0

I use date time formatter to get a date into a string.

LocalDate dateDebutMois = "2021-01-01";

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-YYYY");

String dateToPrint = dateDebutMois.format(dateTimeFormatter);

In dateToPrint I get : "01-2020" instead of "01-2021".

Any idea what I am doing wrong?

geocodezip
  • 158,664
  • 13
  • 220
  • 245

1 Answers1

-1

@AndyTurner's answer for the year was spot on. But your example has problems. For one you can't assign a String to a LocalDate.

Try it like this.

String date = "2021-01-01";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-yyyy");
String s = dateTimeFormatter.format(LocalDate.parse(date));
System.out.println(s);

Prints

01-2021
WJS
  • 36,363
  • 4
  • 24
  • 39