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 –
- LocalDate.parse(CharSequence text) – returns in default yyyy-MM-dd format
- LocalDate.parse(CharSequence text, DateTimeFormatter formatter) – this variant helps to customize LocalDate format using formatter
- Both variants throws java.time.format.DateTimeParseException when,
- For the 1st variant, if the specified date in string-form isn’t in default yyyy-MM-dd format
- 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:
- Java 8 – LocalDate with method details and examples
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
- https://docs.oracle.com/javase/8/docs/api/java/time/format/FormatStyle.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!