144

How can i convert a string to a LocalDate?

I have seen examples like:

LocalDate dt = new LocalDate("2005-11-12");

But my strings are like:

2005-nov-12

5 Answers 5

260

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_FRENCH
LocalDate date = LocalDate.parse("2005-nov-12", formatter);

The syntax is nearly the same though.

3
  • 1
    java.time.format.DateTimeParseException: Text '2005-nov-12' could not be parsed at index 5 Apr 23, 2016 at 18:41
  • 4
    This happens because your default Locale expects the 'N' of 'Nov' to be upper case.
    – hertzi
    Jun 15, 2016 at 14:15
  • See my Answer to a similar Question for more discussion of this approach. Aug 11, 2016 at 20:09
102

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

2
  • Also submitted an edit for the format "yyyy-MMM-dd". YYYY is year of era, and (more egregiously) DD is day of year. Jan 5, 2012 at 16:55
  • the answer is slightly wrong... for joda time, the class to use is DateTimeFormat, not DateTimeFormatter. In other words, call DateTimeFormat.ofPattern("yyyy-MM-dd").parseLocalDate(yourInput); Jul 12, 2019 at 12:13
14

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();
1
  • 6
    Note that this answer refers to Joda, not Java SE.
    – DavidS
    Nov 22, 2019 at 0:30
9

Datetime formatting is performed by the org.joda.time.format.DateTimeFormatter class. Three classes provide factory methods to create formatters, and this is one. The others are ISODateTimeFormat and DateTimeFormatterBuilder.

DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MMM-dd");
LocalDate lDate = new LocalDate().parse("2005-nov-12",format);

final org.joda.time.LocalDate class is an immutable datetime class representing a date without a time zone. LocalDate is thread-safe and immutable, provided that the Chronology is as well. All standard Chronology classes supplied are thread-safe and immutable.

5

DateTimeFormatter has in-built formats that can directly be used to parse a character sequence. It is case Sensitive, Nov will work however nov and NOV wont work:

DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MMM-dd");

try {
    LocalDate datetime = LocalDate.parse(oldDate, pattern);
    System.out.println(datetime); 
} catch (DateTimeParseException e) {
    // DateTimeParseException - Text '2019-nov-12' could not be parsed at index 5
    // Exception handling message/mechanism/logging as per company standard
}

DateTimeFormatterBuilder provides custom way to create a formatter. It is Case Insensitive, Nov , nov and NOV will be treated as same.

DateTimeFormatter f = new DateTimeFormatterBuilder().parseCaseInsensitive()
        .append(DateTimeFormatter.ofPattern("yyyy-MMM-dd")).toFormatter();
try {
    LocalDate datetime = LocalDate.parse(oldDate, f);
    System.out.println(datetime); // 2019-11-12
} catch (DateTimeParseException e) {
     // Exception handling message/mechanism/logging as per company standard
}
1
  • 2
    Welcome to SO, we appreciate your input! Please edit your answer and provide a bit more explanations, how it relates to the question, where you got your solution from, why it works? For more guidance see stackoverflow.com/help/how-to-answer
    – B--rian
    Aug 26, 2019 at 9:31

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.