String to LocalDate

How to convert string to LocalDate ?

I saw examples like:

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

But my lines are like:

 2005-nov-12 
+91
java jodatime
Jan 05 '12 at 16:35
source share
5 answers

Since you are using Joda Time, you should use DateTimeFormatter :

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



If you are using Java 8 or later, see Hertz's answer.

+70
Jan 05 2018-12-12T00:
source share
— -

java.time

Starting with Java 1.8, you can achieve this without an additional library using the java.time classes. See the 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); 

Although the syntax is almost the same.

+169
Mar 20 '14 at 16:15
source share

You may need to switch from DateTime to LocalDate.

 DateTimeFormatter FORMATTER = DateTimeFormat.forPattern("yyyy-MMM-dd"); DateTime dateTime = FORMATTER.parseDateTime("2005-nov-12"); LocalDate localDate = dateTime.toLocalDate(); 
+14
Jan 05 '12 at 16:40
source share

Date and time formatting is done using the org.joda.time.format.DateTimeFormatter class . Three classes provide factory methods for creating formatter, and this is one. The rest 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 timeline is also. All standard timeline classes supplied are thread safe and immutable.

+8
Jan 05 2018-12-12T00:
source share

DateTimeFormatter has built-in formats that can be used directly to analyze character sequences. This is case sensitive, November will work, however, new and NOV will not 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 its own way to create a formatter. This is not case sensitive, November, November and November will be considered the 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 } 
0
Aug 26 '19 at 9:05
source share



All Articles