Languages
[Edit]
EN

Java 8 - how to create custom DateTimeFormatter with DateTimeFormatterBuilder?

4 points
Created by:
Root-ssh
6253

1. Overview

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

DateTimeFormatter has already build-in date time formatters.

We can use DateTimeFormatterBuilder to create our own date time formatter. 

2. DateTimeFormatterBuilder example with YEAR, MONTH, DAY

Code example:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;

public class DateTimeFormatterBuilderExample1 {

    public static void main(String[] args) {

        DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();

        DateTimeFormatter formatter = builder
                //
                .appendLiteral("year: ")
                .appendValue(ChronoField.YEAR)
                //
                .appendLiteral(", month: ")
                .appendValue(ChronoField.MONTH_OF_YEAR)
                //
                .appendLiteral(", day: ")
                .appendValue(ChronoField.DAY_OF_MONTH)
                //
                .toFormatter();

        LocalDateTime localDateTime  = LocalDateTime.now();
        String dateTimeNow = localDateTime.format(formatter);

        // 2019-10-12T15:21:15.096
        System.out.println(localDateTime);

        // year: 2019, month: 10, day: 12
        System.out.println(dateTimeNow);
    }
}

Output:

2019-10-12T15:21:15.096
year: 2019, month: 10, day: 12

3. DateTimeFormatterBuilder example with YEAR, MONTH, DAY, HOUR, MINUTE, MILLI, MICRO

Code example:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;

public class DateTimeFormatterBuilderExample2 {

    public static void main(String[] args) {

        DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();

        DateTimeFormatter formatter = builder.
                //
                appendLiteral("year: ")
                .appendValue(ChronoField.YEAR)
                //
                .appendLiteral(", month: ")
                .appendValue(ChronoField.MONTH_OF_YEAR)
                //
                .appendLiteral(", day: ")
                .appendValue(ChronoField.DAY_OF_MONTH)
                //
                .appendLiteral(", hour: ")
                .appendValue(ChronoField.HOUR_OF_DAY)
                //
                .appendLiteral(", minute: ")
                .appendValue(ChronoField.MINUTE_OF_HOUR)
                //
                .appendLiteral(", second: ")
                .appendValue(ChronoField.SECOND_OF_MINUTE)
                //
                .appendLiteral(", milli: ")
                .appendValue(ChronoField.MILLI_OF_SECOND)
                //
                .appendLiteral(", micro: ")
                .appendValue(ChronoField.MICRO_OF_SECOND)
                //
                .toFormatter();

        LocalDateTime localDateTime  = LocalDateTime.now();
        String dateTimeNow = localDateTime.format(formatter);

        // 2019-10-12T15:21:57.407
        System.out.println(localDateTime);

        // year: 2019, month: 10, day: 12, 
        // hour: 15, minute: 21, second: 57, milli: 407, micro: 407000
        System.out.println(dateTimeNow);
    }
}

Output:

2019-10-12T15:21:57.407
year: 2019, month: 10, day: 12, hour: 15, minute: 21, second: 57, milli: 407, micro: 407000

 

Checkout latest findings & news:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Rockstar community members
Thank you for great work 👍