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
adarshr
  • 61,315
  • 23
  • 138
  • 167
clankill3r
  • 9,146
  • 20
  • 70
  • 126

5 Answers5

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
hertzi
  • 2,809
  • 2
  • 14
  • 13
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

Jake Mayer
  • 25
  • 1
  • 5
fge
  • 119,121
  • 33
  • 254
  • 329
  • Also submitted an edit for the format "yyyy-MMM-dd". YYYY is year of era, and (more egregiously) DD is day of year. – Spencer Kormos Jan 05 '12 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); – Dan Haywood Jul 12 '19 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();
John Deverall
  • 5,954
  • 3
  • 27
  • 37
Spencer Kormos
  • 8,381
  • 3
  • 28
  • 45
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.

Chandra Sekhar
  • 16,256
  • 10
  • 67
  • 90
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
}
kalehmann
  • 4,821
  • 6
  • 26
  • 36
  • 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 https://stackoverflow.com/help/how-to-answer – B--rian Aug 26 '19 at 09:31