String to LocalDate String to LocalDate java java

String to LocalDate


java.time

Since Java 1.8, you can achieve this without an extra library by using the java.time classes. See Tutorial.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MMM-dd");formatter = formatter.withLocale( putAppropriateLocaleHere );  // Locale specifies human language for translating, and cultural norms for lowercase/uppercase and abbreviations and such. Example: Locale.US or Locale.CANADA_FRENCHLocalDate date = LocalDate.parse("2005-nov-12", formatter);

The syntax is nearly the same though.


As you use Joda Time, you should use DateTimeFormatter:

final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MMM-dd");final LocalDate dt = dtf.parseLocalDate(yourinput);

If using Java 8 or later, then refer to hertzi's answer


You may have to go from DateTime to LocalDate.

Using Joda Time:

DateTimeFormatter FORMATTER = DateTimeFormat.forPattern("yyyy-MMM-dd");DateTime dateTime = FORMATTER.parseDateTime("2005-nov-12");LocalDate localDate = dateTime.toLocalDate();