Languages
[Edit]
EN

Java 8 - DateTimeFormatter predefined formatters examples with LocalDate, LocalTime, LocalDateTime, ZonedDateTime

11 points
Created by:
Nikki-Mathews
517

1. Overview

Java 8 introduced DateTimeFormatter to give programmers more control over date time formats.

DateTimeFormatter has a lot of already build-in date time formatters.

With DateTimeFormatter we can use one of:

  1. LocalDate
  2. LocalTime
  3. LocalDateTime
  4. ZonedDateTime

Below we can see some examples of build in DateTimeFormatter with those 4 date / time classes.

2. DateTimeFormatter predefined formatters code examples 

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeFormatterBuildInFormatterExamples {

    public static void main(String[] args) {

        System.out.println("# Example 1");
        // 2019-10-12
        System.out.println(LocalDate.parse("20191012",
                DateTimeFormatter.BASIC_ISO_DATE));

        System.out.println("# Example 2");
        // 11:15:30
        System.out.println(LocalTime.parse("11:15:30",
                DateTimeFormatter.ISO_TIME));

        System.out.println("# Example 3");
        // 2019-10-12T11:15:30
        System.out.println(LocalDateTime.parse("2019-10-12T11:15:30+01:00[Europe/Paris]",
                        DateTimeFormatter.ISO_ZONED_DATE_TIME));

        System.out.println("# Example 4");
        // 2019-10-12T11:15:10
        System.out.println(LocalDateTime.parse("Sat, 12 Oct 2019 11:15:10 GMT",
                DateTimeFormatter.RFC_1123_DATE_TIME));

        System.out.println("# Example 5");
        // 2019-10-12T11:15:30
        System.out.println(LocalDateTime.parse("2019-10-12T11:15:30",
                DateTimeFormatter.ISO_LOCAL_DATE_TIME));

        System.out.println("# Example 6");
        // 2019-10-12T11:15:30
        System.out.println(LocalDateTime.parse("2019-10-12T11:15:30+02:00",
                DateTimeFormatter.ISO_OFFSET_DATE_TIME));

        System.out.println("# Example 7");
        // 2019-10-12T11:15:30+02:00[Europe/Paris]
        System.out.println(ZonedDateTime.parse("2019-10-12T11:15:30+01:00[Europe/Paris]",
                DateTimeFormatter.ISO_ZONED_DATE_TIME));

        System.out.println("# Example 8");
        // 2019-10-12T11:15:30+02:00
        System.out.println(ZonedDateTime.parse("2019-10-12T11:15:30+02:00",
                DateTimeFormatter.ISO_OFFSET_DATE_TIME));
    }
}

Output:

# Example 1
2019-10-12
# Example 2
11:15:30
# Example 3
2019-10-12T11:15:30
# Example 4
2019-10-12T11:15:10
# Example 5
2019-10-12T11:15:30
# Example 6
2019-10-12T11:15:30
# Example 7
2019-10-12T11:15:30+02:00[Europe/Paris]
# Example 8
2019-10-12T11:15:30+02:00

3. DateTimeFormatter - Predefined Formatters

Formatter Description Example
BASIC_ISO_DATE Basic ISO date '20111203'
ISO_LOCAL_DATE ISO Local Date '2011-12-03'
ISO_OFFSET_DATE ISO Date with offset '2011-12-03+01:00'
ISO_DATE ISO Date with or without offset '2011-12-03+01:00'; '2011-12-03'
ISO_LOCAL_TIME Time without offset '10:15:30'
ISO_OFFSET_TIME Time with offset '10:15:30+01:00'
ISO_TIME Time with or without offset '10:15:30+01:00'; '10:15:30'
ISO_LOCAL_DATE_TIME ISO Local Date and Time '2011-12-03T10:15:30'
ISO_OFFSET_DATE_TIME Date Time with Offset 2011-12-03T10:15:30+01:00'
ISO_ZONED_DATE_TIME Zoned Date Time '2011-12-03T10:15:30+01:00[Europe/Paris]'
ISO_DATE_TIME Date and time with ZoneId '2011-12-03T10:15:30+01:00[Europe/Paris]'
ISO_ORDINAL_DATE Year and day of year '2012-337'
ISO_WEEK_DATE Year and Week 2012-W48-6'
ISO_INSTANT Date and Time of an Instant '2011-12-03T10:15:30Z'
RFC_1123_DATE_TIME RFC 1123 / RFC 822 'Tue, 3 Jun 2008 11:05:30 GMT'

 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join