java.time.format.DateTimeFormatterBuilder Class in Java - GeeksforGeeks

25 Oct, 2021
DateTimeFormatterBuilder Class is a builder class that is used to create date-time formatters. DateTimeFormatter is used as Formatter for printing and parsing date-time objects. DateTimeFormatterBuilder allows a DateTimeFormatter to be created. DateTimeFormatterBuilder is used for constructing formatters which are then used to print or parse. The formatters are built by appending specific fields or other formatters to an instance of this builder. If we want to create our own DateTimeFormatter object, then java.time.format.DateTimeFormatterBuilder will help. All date-time formatters are created ultimately using this builder.
Syntax:
public final class DateTimeFormatterBuilder
extends Object
Let us first discuss the constructors of this class before diving into the methods of this class 
Methods:
Let us do discuss the methods present in this class that is declared in this class as follows:
clone()
equals()
finalize()
getClass()
hashCode()
notify()
notifyAll()
toString()
wait() 
They are pictorially depicted below in tabular format in alphabetical alongside the action performed by them which are as follows:
Now let us implement by invoking a few of the methods of this class with help of clean java programs to get a better understanding of the class via implementing its methods. 
Implementation:
In LocalDateTime class, there are three types of now() method depending upon the parameters passed to it. now() method of a LocalDateTime class used to obtain the current date-time from the system clock in the default time-zone. This method will return LocalDateTime based on the system clock with the default time-zone to obtain the current date-time.
Example 1 
Java
// Java Program to illustrate DateTimeFormatterBuilder class// by invoking appendValue() Method of this class // Importing required classes from respective packagesimport java.io.*;import java.lang.*;import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;import java.time.format.DateTimeFormatterBuilder;import java.time.format.TextStyle;import java.time.temporal.ChronoField; // Main classpublic class GFG {     // Main driver method    public static void main(String[] args)    {         // Creating an object of this class        DateTimeFormatterBuilder builder            = new DateTimeFormatterBuilder();         // Now creating object of DateTimeFormatter class        // over which appendValue() method is invoked        DateTimeFormatter formatter            = builder.appendLiteral("Day is:")                  .appendValue(ChronoField.DAY_OF_MONTH)                  .appendLiteral(", month is:")                  .appendValue(ChronoField.MONTH_OF_YEAR)                  .toFormatter();         // Creating object of LocalDateTime class        // invoking now() method over it        LocalDateTime dateTime = LocalDateTime.now();         String str = dateTime.format(formatter);         // Print and display the day and month        System.out.println(str);    }}
Day is:7, month is:7
Example 2
optionalStart() and optionalEnd() method
Java
// Java program to illustrate DateTimeFormatterBuilder// Using optionalStart() and optionalEnd() Methods // Importing required librariesimport java.io.*;import java.lang.*;import java.time.LocalDate;import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;import java.time.format.DateTimeFormatterBuilder;import java.time.temporal.ChronoField; // Main classpublic class GFG {     // Main driver methods    public static void main(String[] args)    {         // Creating an object of DateTimeFormatter class        DateTimeFormatter parser            = new DateTimeFormatterBuilder()                  .appendPattern("[yyyy][yyyyMM][yyyyMMdd]")                  .optionalStart()                  .parseDefaulting(                      ChronoField.MONTH_OF_YEAR, 1)                  .parseDefaulting(ChronoField.DAY_OF_MONTH,                                   1)                  .optionalEnd()                  .toFormatter();         // Print and display statements         // Execute if only year is given in parameter        System.out.println(            parser.parse("2021", LocalDate::from));         // Execute if year and month is given        System.out.println(            parser.parse("202106", LocalDate::from));         // Execute if year, month and date is given        System.out.println(            parser.parse("20210631", LocalDate::from));    }}
2021-01-01
2021-06-01
2021-06-30
Example 3
Java
// Java program to illustrate DateTimeFormatterBuilder class // Importing required classes from respective packagesimport java.io.*;import java.lang.*;import java.time.LocalDate;import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;import java.time.format.DateTimeFormatterBuilder;import java.time.format.TextStyle;import java.time.temporal.ChronoField;import java.util.Locale; // Main class// DateTimeFormatterBuilderExampleclass GFG {     // Main driver method    public static void main(String[] args)    {         // Creat        LocalDate date = LocalDate.of(2021, 6, 30);         // Creating object of DateTimeFormatter class to        //         // Object 1        DateTimeFormatter formatter            = DateTimeFormatter.ofPattern("dd/MM/yyyy");         // Object 2        DateTimeFormatter italianFormatter            = DateTimeFormatter.ofPattern("d. MMMM yyyy",                                          Locale.ITALIAN);         // Print and display date in format XX-XX-XX        System.out.println(            date.format(DateTimeFormatter.ISO_LOCAL_DATE));         // Print and display date in format XX/XX/XX        System.out.println(date.format(formatter));         // Print and display Italian date formatter        System.out.println(date.format(italianFormatter));         // Object 3        DateTimeFormatter complexFormatter            = new DateTimeFormatterBuilder()                  .appendText(ChronoField.DAY_OF_MONTH)                  .appendLiteral(". ")                  .appendText(ChronoField.MONTH_OF_YEAR)                  .appendLiteral(" ")                  .appendText(ChronoField.YEAR)                  .parseCaseInsensitive()                  .toFormatter(Locale.US);         // Print ad display US date formatter        System.out.println(date.format(complexFormatter));    }}
2021-06-30
30/06/2021
30. giugno 2021
30. June 2021
anikaseth98
Java-Classes
Java-Date-Time
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Hashtable in Java
Constructors in Java
Different ways of Reading a text file in Java
Comparator Interface in Java with Examples
Java Math random() method with Examples
HashMap containsKey() Method in Java
How to Create Array of Objects in Java?
Convert Double to Integer in Java
Iterating over ArrayLists in Java
Generating random numbers in Java