Handling dates (and time) is never easy and time-consuming. Moreover, its support by Java has been inadequate. If you find yourself longing for some libraries to ease the pain, go ahead because you are not alone. For pre-Java 8, one of the best options is JodaTime. Then came Java 8 with java.time API, a project that has been led jointly by the author of Joda-Time (Stephen Colebourne) and Oracle, under JSR 310.
For more details, please read Java SE 8 Date and Time by Ben Evans and Richard Warburton.
Back to the topic. Here are ways how to convert a date string in a particular format to a LocalDate object.
Using JodaTime
1 2 | final DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyyMMdd"); final LocalDate dt = dtf.parseLocalDate(dateInYYYYMMDD); |
Using Java 8 java.time API
1 2 | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); LocalDate date = LocalDate.parse("20160212", formatter); |
Note that you still need to include any JodaTime dependencies unless you are using Java 8 all the way.