Site icon Automation Dojos

Convert String to java.time.LocalDate with Formatter

The LocalDate class has an overloaded parse() method. In addition to the String Date, it accepts a DateTimeFormatter instance that specifies the format of the input date. So, this can be used in case the String date is not in the yyyy-MM-dd format. The following code demonstrates this:

The following code demonstrates this:

In this case, the String date is in the dd/MM/YYYY format.  A DateTimeFormatter instance is created with this pattern. The parse() method is then invoked with the String date and the DateTimeFormatter instance. So, this code prints the following output:

2015-06-12

You can check out the API documentation for DateTimeFormatter in order to understand the patterns that need to be specified for various date formats.

Exit mobile version