-4

What is the best way to convert date in String format "YYYY-MM" to Date object in Java apart from String parsing.

What I tried is below.

 String expirationDateArr[] = dateStr.split("-");

Then extract month and year to create the Date object.

BigDataLearner
  • 1,388
  • 4
  • 19
  • 40

1 Answers1

7

Don't use Date. Since Java 8 we have time package which includes YearMonth class. With it your code can be as simple as

YearMonth ym = YearMonth.parse("2017-10");
Pshemo
  • 122,468
  • 25
  • 185
  • 269