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.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) {

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(
    at java.time.format.DateTimeFormatter.parse(
    at java.time.LocalDate.parse(
    at java.time.LocalDate.parse(
    at org.kodejava.example.datetime.DateTimeParseDemo.main(
Caused by: java.time.DateTimeException: Invalid date 'FEBRUARY 31'
    at java.time.LocalDate.create(
    at java.time.LocalDate.of(
    at java.time.chrono.IsoChronology.resolveYMD(
    at java.time.chrono.IsoChronology.resolveYMD(
    at java.time.chrono.AbstractChronology.resolveDate(
    at java.time.chrono.IsoChronology.resolveDate(
    at java.time.chrono.IsoChronology.resolveDate(
    at java.time.format.Parsed.resolveDateFields(
    at java.time.format.Parsed.resolveFields(
    at java.time.format.Parsed.resolve(
    at java.time.format.DateTimeParseContext.toResolved(
    at java.time.format.DateTimeFormatter.parseResolved0(
    at java.time.format.DateTimeFormatter.parse(
    ... 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.

Leave a Reply

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