-1

I've got:

String str = "2016-01-05" ;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = LocalDate.parse(str, formatter);

Now, the variable "date" contains the date "2016-01-05". How do I extract "2016-01" from that date and put it again into the variable "date" ?

assylias
  • 321,522
  • 82
  • 660
  • 783
Marco
  • 322
  • 1
  • 3
  • 15

1 Answers1

4

If you are only interested in the year and the month, the best class would be YearMonth. A LocalDate requires a day.

You can create an instance with:

YearMonth ym = YearMonth.from(date);
assylias
  • 321,522
  • 82
  • 660
  • 783
  • ym will be of type YearMonth, not LocalDate, no? can I do date = ym ? – Marco Jul 01 '16 at 13:54
  • 1
    @Marco yes it will and no you can't. A LocalDate has a day, you can't remove it, that's the way the class was designed. You should maybe give a bit more context if a YearMonth is not useful for your problem. – assylias Jul 01 '16 at 13:55