In this post, we will see a few examples of how to convert String to LocalDate in Java.
Some of the examples have been done using Lombok.
String in ISO_LOCAL_DATE
When the string is in ISO_LOCAL_DATE, no parsing is necessary, and it can be converted directly.
@Slf4j public class IsoLocalDateExample { public static void main(String[] args) { String date = "1983-05-10"; LocalDate localDate = LocalDate.parse(date); log.debug(localDate.toString()); } }
OUTPUT: 1983-05-10
String with format
When we are going to use a date with a specific format, the type of date format must be defined:
Slf4j public class StringDateFormatExample { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); String date = "05/10/1983"; LocalDate localDate = LocalDate.parse(date, formatter); log.debug(localDate.toString()); log.debug("Date is es: " + formatter.format(localDate).toString()); } }
Output: 2016-08-16 Date is: 16/08/2016
String with Month in Letter and Locale
In the following example, we will transform a String in which the Month comes with a letter and the Spanish Locale. In this case, we have to establish that the DateTimeFormatter has the Locale.US by default to avoid exceptions.
@Slf4j public class StringDateMonthLocaleExample { public static void main(String[] args) { Locale.setDefault(Locale.ES); DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy", Locale.US); String date = "01-Aug-1983"; LocalDate date = LocalDate.parse(date, dateFormatter); log.debug(date.toString()); log.debug(dateFormatter.format(date).toString()); } }
Output: 1983-08-01 //ISO_LOCALDATE 01-Aug-1983
String with abbreviated Weekday and Month
We must format a string with abbreviated Month and Weekday, both in English, as follows when we receive it:
@Slf4j public class StringDateMonthWeekExample { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E, MMM d yyyy", Locale.US); String stringDate = "Friday, Apr 01 1983"; LocalDate localDate = LocalDate.parse(stringDate, formatter); log.debug(localDate.toString()); log.debug(formatter.format(localDate).toString()); } }
Output: 1983-04-01 Mon, Apr 1 1983
Convert String to LocalDateTime
In the following example, we will convert a String with LocalDateTime format to LocalDate. The difference with the previous examples is that now, we will indicate the time.
@Slf4j public class StringToLocalDateTime { public static void main(String[] args) { DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("EEEE, MMM d, yyyy hh:mm:ss a", Locale.US); String stringDate = "Friday, Apr 1, 1983 10:01:00 AM"; LocalDateTime localDateTime = LocalDateTime.parse(stringDate, dateFormatter); log.debug(localDateTime.toString()); log.debug(dateFormatter.format(localDateTime).toString()); } }
Output: 1983-04-1T10:01:00 Friday, Apr 1, 1983 10:01:00 AM
Convert String with LocalDateTime format to LocalDate
Starting from the previous example, we are going to convert the input String of type LocalDateTime to LocalDate. To do this, we will first transform it to LocalDateTime, as in the previous example, and then we will perform its transformation to LocalDate.
@Slf4j public class StringToLocalDateTimeToLocalDate { public static void main(String[] args) { DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("EEEE, MMM d, yyyy hh:mm:ss a", Locale.US); String stringDate = "Friday, Apr 1, 1983 10:01:00 AM"; LocalDateTime localDateTime = LocalDateTime.parse(stringDate, dateFormatter); log.debug(localDateTime.toString()); log.debug(dateFormatter.format(localDateTime).toString()); log.debug(dateFormatter.format(localDateTime).toLocalDate().toString()); log.debug(localDateTime.toLocalDate().toString()); } }
Output: 1983-04-1T10:01:00 Friday, Apr 1, 1983 10:01:00 AM Friday, Apr 1, 1983 1983-04-1
Exception with LocalDate
Now we are going to provoke an exception in the transformation of a date, we are going to try to transform a date that has day 33. To do this, we will make use of the DateTimeParseException exception:
@Slf4j public class LocalDateExceptionExample { public static void main(String[] args) { try { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); String date = "33/10/1983"; LocalDate localDate = LocalDate.parse(date, formatter); } catch (DateTimeParseException e) { log.error("Error parsing the date"); } } }
Conclusión
In this article, we have seen different examples of how to convert String to LocalDate in Java, which will be very useful to work with different LocalDate formats.
Other articles that may interest you
Convert Date to LocalDate or LocalDateTime
If you need more information, you can leave us a comment or send an email to refactorizando.web@gmail.com You can also contact us through our social media channels on Facebook or twitter and we will be happy to assist you!!