Java 8 – How to parse LocalDate in String form ?

In this article, we will learn how to parse LocalDate and Date in String form using LocalDate.parse() method provided in Java 1.8 version

1. Parse java.time.LocalDate :

  • Sometimes, we need to parse date passed in String-form to LocalDate, for that we can use LocalDate.parse() method which will return LocalDate in yyyy-MM-dd format
  • There are 2 variants of LocalDate.parse() method –
    1. LocalDate.parse(CharSequence text) – returns in default yyyy-MM-dd format
    2. LocalDate.parse(CharSequence text, DateTimeFormatter formatter) – this variant helps to customize LocalDate format using formatter
  • Both variants throws java.time.format.DateTimeParseException when,
    1. For the 1st variant, if the specified date in string-form isn’t in default yyyy-MM-dd format
    2. For the 2nd variant, if specified date in string-form isn’t matching with custom pattern/format specified in the DateTimeFormatter
  • Finally, print LocalDate to the console for both the variants

ParseLocalDate.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class ParseLocalDate {

	public static void main(String[] args) {

		// 1. Republic-Day date
		String republicDayDate = "1950-01-26";


		// 1.1 convert/parse to dateInString to LocalDate in default format
		LocalDate republicDate = LocalDate.parse(republicDayDate);
		System.out.println("Parsed Republic-Day date is - " + republicDate);



		// 2. Independence-Day date
		String independenceDayDate = "15/08/1947";


		// 2.1 custom format
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");


		// 2.2 convert/parse to dateInString to LocalDate in custom format
		LocalDate independenceDate = LocalDate.parse(independenceDayDate, dateTimeFormatter);
		System.out.println("\nParsed Independence-Day date is - " + independenceDate);
	}
}

Output:

Parsed Republic-Day date is - 1950-01-26

Parsed Independence-Day date is - 1947-08-15

2. Parse java.util.Date :

  • Default format for java.util.Date is E MMM dd HH:mm:ss zzz yyyy
  • Parsing Date in custom formats is simple using DateFormat & SimpleDateFormat classes as shown in the below example
  • Finally, print Date to the console for both default and custom formats

ParseDate.java

package in.bench.resources.java8.localdate.examples;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ParseDate {

	public static void main(String[] args) {

		// 1. current date
		Date date = new Date();
		System.out.println("Date/Time in "
				+ "default format :- \n" + date);


		// 1.1 format
		DateFormat dateFormat =  new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		String str = dateFormat.format(date);
		System.out.println("\nFormatted Date/Time in "
				+ "dd-MM-yyyy HH:mm:ss format :- \n" + str);
	}
}

Output:

Date/Time in default format :- 
Thu Jul 28 11:21:02 IST 2022

Formatted Date/Time in dd-MM-yyyy HH:mm:ss format :- 
28-07-2022 11:21:02

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert String to LocalDate ?
Java 8 – How to form LocalDate passing Year, Month and Day fields ?