Formatting with Java DateTimeFormatter

Learn to use DateTimeFormatter for formatting ZonedDateTime, LocalDateTime, LocalDate and LocalTime to string with predefined and custom patterns.

The patterns created from DateTimeFormatter are thread-safe and immutable and can be used as many times as necessary.

1. Create DateTimeFormatter

1.1. Simple Formatters

We can create a very simple DateTimeFormatter instance in 3 ways:

  1. Using predefined constants, such as ISO_LOCAL_DATE
  2. Creating custom patterns using pattern latters and ofPattern() method
  3. Using localized styles with FormatStyle, such as LONG or MEDIUM
//Use inbuilt pattern constants
DateTimeFormatter inBuiltFormatter1 = DateTimeFormatter.ISO_DATE_TIME;
DateTimeFormatter inBuiltFormatter2 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;

//Define your own custom patterns
DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mma z");

//Using FormatStyle
DateTimeFormatter customFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);

1.2. Using DateTimeFormatterBuilder

Use  DateTimeFormatterBuilder to create a more complex formatter instance.

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
                .optionalStart()
                .appendPattern(".SSS")
                .optionalEnd()
                .optionalStart()
                .appendZoneOrOffsetId()
                .optionalEnd()
                .optionalStart()
                .appendOffset("+HHMM", "0000")
                .optionalEnd()
                .toFormatter();

2. Formatting with DateTimeFormatter

The DateTimeFormatter class provides mainly one method to format a given date-time instance of any type.

  • dateTime.format(formatter) : Formats the dateTime instance using the specified formatter.

2.1. Formatting ZonedDateTime

Java example to format ZonedDateTime to string with DateTimeFormatter.

//Create formatter
DateTimeFormatter FOMATTER = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mm a z");

//Zoned datetime instance
ZonedDateTime zdt = ZonedDateTime.now();

//Get formatted String
String zdtString = FOMATTER.format(zdt);

System.out.println(zdtString);		// 07/15/2018 at 02:51 PM IST

2.2. Formatting LocalDateTime

LocalDate does not have a timezone part so create the pattern accordingly.

//Create formatter
DateTimeFormatter FOMATTER = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mm a");

//Local date time instance
LocalDateTime localDateTime = LocalDateTime.now();

//Get formatted String
String ldtString = FOMATTER.format(localDateTime);

System.out.println(ldtString);		// 07/15/2018 at 02:49 PM

2.3. Formatting LocalDate

LocalDate does not have time and timezone parts. So create the pattern accordingly.

//Create formatter
DateTimeFormatter FOMATTER = DateTimeFormatter.ofPattern("MM/dd/yyyy");

//Local date instance
LocalDate localDate = LocalDate.now();

//Get formatted String
String dateString = FOMATTER.format(localDate);

System.out.println(dateString);		//07/15/2018

2.4. Formatting LocalTime

LocalTime does not have the date and timezone parts so create the pattern accordingly.

//Create formatter
DateTimeFormatter FOMATTER = DateTimeFormatter.ofPattern("hh:mm a");

//Local time instance
LocalTime localTime = LocalTime.now();

//Get formatted String
String localTimeString = FOMATTER.format(localTime);

System.out.println(localTimeString);		// 02:53 PM

3. Useful Formatting Patterns

PatternExample
yyyy-MM-dd (ISO)“2018-07-14”
dd-MMM-yyyy“14-Jul-2018”
dd/MM/yyyy“14/07/2018”
E, MMM dd yyyy“Sat, Jul 14 2018”
h:mm a“12:08 PM”
EEEE, MMM dd, yyyy HH:mm:ss a“Saturday, Jul 14, 2018 14:31:06 PM”
yyyy-MM-dd'T'HH:mm:ssZ“2018-07-14T14:31:30+0530”
hh 'o''clock' a, zzzz“12 o’clock PM, Pacific Daylight Time”
K:mm a, z“0:08 PM, PDT”

Happy Learning !!

Sourcecode Download

Leave a Reply

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.