Convert String to Date in Java

thumbnail

In this tutorial, you will read learn to convert string to date in Java using formatter. The first example is converting string to date using predefined formatters.

Example 1: Convert String to Date using predefined formatters

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

public class StringtoDatePredefined {
    public static void main(String[] args) {
        String string = "2019-09-29";
        LocalDate date = LocalDate.parse(string, DateTimeFormatter.ISO_DATE);
        System.out.println("Date using predefined formatter: "+date);
    }
}

The outpull will be:

Date using predefined formatter: 2019-09-29

From the above program, we’ve used the predefined formatter ISO_DATE that takes string in the format “2019-09-29” or “2019-09-29+09:37”. The localDate’s parse() function parses the given string using the given formatter, in this program we use ISO_DATE. You can also use another formatters, such as BASIC_ISO_DATE, ISO_DATE_TIME, ISO_INSTANT, ISO_LOCAL_DATE, ISO_LOCAL_DATE_TIME, ISO_LOCAL_TIME, ISO_OFFSET_DATE, ISO_OFFSET_DATE_TIME, ISO_OFFSET_TIME, ISO_ORDINAL_DATE, ISO_TIME, ISO_WEEK_DATE, ISO_ZONED_DATE_TIME, and RFC_1123_DATE_TIME. You can see the description in the below table.

Formatter

Description

Example

BASIC_ISO_DATE

Basic ISO date

‘20111203’

ISO_DATE

ISO Date with or without offset

‘2011-12-03+01:00’;

‘2011-12-03’

ISO_DATE_TIME

Date and time with ZoneId

‘2011-12-03T10:15:30

+01:00[Europe/Paris]’

ISO_INSTANT

Date and Time of an Instant

‘2011-12-03T10:15:30Z’

ISO_LOCAL_DATE

ISO Local Date

‘2011-12-03’

ISO_LOCAL_DATE_TIME

ISO Local Date and Time

‘2011-12-03T10:15:30’

ISO_LOCAL_TIME

Time without offset

’10:15:30′

ISO_OFFSET_DATE

ISO Date with offset

‘2011-12-03+01:00’

ISO_OFFSET_DATE_TIME

Date Time with Offset

‘2011-12-03T10

:15:30+01:00′

ISO_OFFSET_TIME

Time with offset

’10:15:30+01:00′

ISO_ORDINAL_DATE

Year and day of year

‘2012-337’

ISO_TIME

Time with or without offset

’10:15:30+01:00′;

’10:15:30′

ISO_WEEK_DATE

Year and Week

‘2012-W48-6′

ISO_ZONED_DATE_TIME

Zoned Date Time

‘2011-12-03T10:15:30

+01:00[Europe/Paris]’

RFC_1123_DATE_TIME

RFC 1123 / RFC 822

‘Tue, 3 Jun 2008

11:05:30 GMT’

ofLocalizedDate

(dateStyle)

Formatter with date style from the locale

‘2011-12-03’

ofLocalizedTime

(timeStyle)

Formatter with time style from the locale

’10:15:30′

ofLocalizedDateTime

(dateTimeStyle)

Formatter with a style for date and time from the locale

‘3 Jun 2008 11:05:30’

ofLocalizedDateTime

(dateStyle, timeStyle)

Formatter with date and time styles from the locale

‘3 Jun 2008 11:05’

Example 2: Convert String to Date using pattern formatters

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

public class StringtoDatePattern {
    public static void main(String[] args) {
        String string = "September 29, 2019";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH);
        LocalDate date = LocalDate.parse(string, formatter);
        System.out.println("Date using pattern formatter: "+date);
    }
}

The output will be:

Date using predefined formatter: 2019-09-29

We can see from the above program, our date format is defined in “MMMM d, yyyy” format. So, we create a formatter of the given pattern. Then, we can parse the date using LocalDate.parse() function and get the LocalDate object to print. For another awesome tutorial, you can find it in this website.

Back To Top