How do I parse a text string into date and time?

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.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 "2021-09-12" into LocalDate instance.
        LocalDate date = LocalDate.parse("2021-09-12");

        // Parse string "17:51:15: into LocalTime instance.
        LocalTime time = LocalTime.parse("17:51:15");

        // Parse string "2021-09-12T17:51:15" into LocalDateTime instance.
        LocalDateTime dateTime = LocalDateTime.parse("2021-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 parsed, a RuntimeException of type
            // DateTimeParseException will be thrown.
            LocalDate date1 = LocalDate.parse("2021-02-31");
            System.out.println("date1     = " + date1);
        } catch (DateTimeParseException e) {
            e.printStackTrace();
        }
    }
}

Running this code snippet will produce the following result:

date     = 2021-09-12
time     = 17:51:15
dateTime = 2021-09-12T17:51:15
java.time.format.DateTimeParseException: Text '2021-02-31' could not be parsed: Invalid date 'FEBRUARY 31'
    at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2023)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1958)
    at java.base/java.time.LocalDate.parse(LocalDate.java:430)
    at java.base/java.time.LocalDate.parse(LocalDate.java:415)
    at org.kodejava.datetime.DateTimeParseDemo.main(DateTimeParseDemo.java:27)
Caused by: java.time.DateTimeException: Invalid date 'FEBRUARY 31'
    at java.base/java.time.LocalDate.create(LocalDate.java:461)
    at java.base/java.time.LocalDate.of(LocalDate.java:273)
    at java.base/java.time.chrono.IsoChronology.resolveYMD(IsoChronology.java:654)
    at java.base/java.time.chrono.IsoChronology.resolveYMD(IsoChronology.java:126)
    at java.base/java.time.chrono.AbstractChronology.resolveDate(AbstractChronology.java:442)
    at java.base/java.time.chrono.IsoChronology.resolveDate(IsoChronology.java:586)
    at java.base/java.time.chrono.IsoChronology.resolveDate(IsoChronology.java:126)
    at java.base/java.time.format.Parsed.resolveDateFields(Parsed.java:365)
    at java.base/java.time.format.Parsed.resolveFields(Parsed.java:272)
    at java.base/java.time.format.Parsed.resolve(Parsed.java:259)
    at java.base/java.time.format.DateTimeParseContext.toResolved(DateTimeParseContext.java:331)
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2058)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
    ... 3 more

As we can see from the output above, parsing a text string of "2021-02-31" give us a DateTimeParseException because the 31 of February is not a valid date.

Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.