Along with lambda expression, virtual methods, streams and many other nice features, Java 8 has also updated a new Date and Time API which is maintained under the JSR-310. One of the greatest on this new API is that all the date and time related APIs are unified under the same package java.time.
In my previous articles I have explained about the LocalDate , LocalTime and few other APIs introduced with the Java 8 version.This example demonstrates how to use DateTimeFormatterBuilder class.
If you have any clarifications, please write it in the comments section or post it in our facebook page. You can read more articles on Java 8 here.
Java DateTimeFormatterBuilder
In my previous article I have explained about the Java DateTimeFormatter class. This is the most commonly used formatting class for the simple formatting of date/time values. What if when you are trying to create your own custom formatted object. Java 8 defines another class DateTimeFormatterBuilder in the java.time.format package for creating the custom formatted objects. In fact, all the DateTimeFormatter objects are created using this class.
DateTimeFormatterBuilder defines several methods like appendInstant(),appendLiteral(),appendPattern(), etc. for modifying the existing pattern and returns the new DateTimeFormatter object. This example demonstrates how to use DateTimeFormatterBuilder for creating/modifying the patterns.
DateTimeFormatterBuilder – Methods
- appendLiteral(String literal) – This method will append a string literal to the formatting object. This string literal will be output while printing the result. This string literal can be added anywhere in the formatted string
- appendValue(TemporalField field) – This method appends the date / time values to the formatter. This also has the overloaded methods with few extra arguments to provide more flexibility.
- append(DateTimeFormatter formatter) – This method just like appending the entire content of the existing formatter to the builder.
- appendZoneId() – This method appends the zone id to the formatter.
- parseCaseSensitive() – This method will change the parsing to case sensitive.
- toFormatter() – This is the final step/method that should be called for creating the DateTimeFormatter object. This method appends all the values added to the formatter and create a new formatter object.
DateTimeFormatterBuilder – Example
package javabeat.net; import java.time.LocalDate; import java.time.Month; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.format.TextStyle; import java.time.temporal.ChronoField; /** * Java 8 Date Time API - DateTimeFormatterBuilder Example * * @author www.javabeat.net * */ public class DateTimeFormatterBuilderExample { public static void main(String[] args) { DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendLiteral("New Year in ").appendValue(ChronoField.YEAR) .appendLiteral(" is on ") .appendText(ChronoField.DAY_OF_WEEK, TextStyle.FULL_STANDALONE) .toFormatter(); LocalDate localDate1 = LocalDate.of(2015, Month.JANUARY, 1); LocalDate localDate2 = LocalDate.of(2016, Month.JANUARY, 1); LocalDate localDate3 = LocalDate.of(2017, Month.JANUARY, 1); String formattedString1 = localDate1.format(formatter); String formattedString2 = localDate2.format(formatter); String formattedString3 = localDate3.format(formatter); System.out.println("DateTimeFormatterBuilder Demo"); System.out.println("-----------------------------"); System.out.println(formattedString1); System.out.println(formattedString2); System.out.println(formattedString3); } }
Output for the above program will be:
DateTimeFormatterBuilder Demo ----------------------------- New Year in 2015 is on Thursday New Year in 2016 is on Friday New Year in 2017 is on Sunday