In this code snippet example you will learn how to parse a text string into an instance of LocalDate
, LocalTime
and LocalDateTime
. All of these classes provide a parse()
method that accept an argument of text string that represent a valid date and time information and convert it into the corresponding object.
If the text string passed into the parse()
method is not representing a valid date or time information this method throws a RuntimeException
of type DateTimeParseException
exception. If you want to handle this exception then you should wrap your code inside a try-catch block.
Let’s see the code snippet below as an example:
package org.kodejava.example.datetime;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeParseException;
public class DateTimeParseDemo {
public static void main(String[] args) {
// Parse string "2014-09-12" into LocalDate instance.
LocalDate date = LocalDate.parse("2014-09-12");
// Parse string "17:51:15: into LocalTime instance.
LocalTime time = LocalTime.parse("17:51:15");
// Parse string "2014-09-12T17:51:15" into LocalDateTime instance.
LocalDateTime dateTime = LocalDateTime.parse("2014-09-12T17:51:15");
System.out.println("date = " + date);
System.out.println("time = " + time);
System.out.println("dateTime = " + dateTime);
try {
// When the string cannot be parse a RuntimeException of type
// DateTimeParseException will be thrown.
LocalDate date1 = LocalDate.parse("2014-02-31");
System.out.println("date1 = " + date1);
} catch (DateTimeParseException e) {
e.printStackTrace();
}
}
}
Running this code snippet will produce the following result:
date = 2014-09-12
time = 17:51:15
dateTime = 2014-09-12T17:51:15
java.time.format.DateTimeParseException: Text '2014-02-31' could not be parsed: Invalid date 'FEBRUARY 31'
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
at java.time.LocalDate.parse(LocalDate.java:400)
at java.time.LocalDate.parse(LocalDate.java:385)
at org.kodejava.example.datetime.DateTimeParseDemo.main(DateTimeParseDemo.java:35)
Caused by: java.time.DateTimeException: Invalid date 'FEBRUARY 31'
at java.time.LocalDate.create(LocalDate.java:431)
at java.time.LocalDate.of(LocalDate.java:269)
at java.time.chrono.IsoChronology.resolveYMD(IsoChronology.java:560)
at java.time.chrono.IsoChronology.resolveYMD(IsoChronology.java:123)
at java.time.chrono.AbstractChronology.resolveDate(AbstractChronology.java:472)
at java.time.chrono.IsoChronology.resolveDate(IsoChronology.java:492)
at java.time.chrono.IsoChronology.resolveDate(IsoChronology.java:123)
at java.time.format.Parsed.resolveDateFields(Parsed.java:351)
at java.time.format.Parsed.resolveFields(Parsed.java:257)
at java.time.format.Parsed.resolve(Parsed.java:244)
at java.time.format.DateTimeParseContext.toResolved(DateTimeParseContext.java:331)
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1953)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849)
... 3 more
As we can see from the output above, parsing a text string of "2014-02-31"
give us a DateTimeParseException
because the 31 of February is not a valid date.
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020
- How do I get a list of all TimeZones Ids using Java 8? - April 25, 2020