Languages
[Edit]
EN

Java 8 - how to create custom DateTimeFormatter with DateTimeFormatterBuilder?

4 points
Created by:
Root-ssh
175450

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

 

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