-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,198
  • 3
  • 18
  • 35

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
  • 118,400
  • 24
  • 176
  • 257