0

I need to get the month and the year from a LocalDate

        String moisExtraction = LocalDate.now().minusMonths(1).toString();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
        LocalDate dateExtraction = LocalDate.parse(moisExtraction, formatter);

But i am always getting a parse error.

I am a bigenner with java and i did not found the solution for my problem.

sr123
  • 85
  • 1
  • 11
  • you mean , you want to get LocalDate in yyyyMM format ? – Pramod Apr 20 '21 at 08:45
  • the string is something like `2021-04-20`, it cannot be parsed as `yyyyMM` - you already have a `LocalDate`, which is a combination of year, month and day. If you want it **formatted** as `yyyyMM`, use `formatter.format(l)` –  Apr 20 '21 at 08:46
  • yes exactly, I want to get LocalDate in yyyyMM please – sr123 Apr 20 '21 at 08:46
  • You cannot convert string yyyyMM to LocalDate because date is missing – Pramod Apr 20 '21 at 08:47
  • you mean that i need to consider my yyyyMM as a string not a localdate ? i want to say i cannot use my yyyyMM as a localdate so i am obliged to use it as string – sr123 Apr 20 '21 at 08:48
  • Yes @sr123. It cannot be LocalDate – Pramod Apr 20 '21 at 08:49
  • again, use `formatter.format(LocalDate.now().minusMonths(1))` (parse: convert string to some object; format: convert some object to string (representation)) –  Apr 20 '21 at 08:49

0 Answers0