Using DateTimeFormatter In Java

By bhagwatchouhan
Using DateTimeFormatter In Java

Java 8 has introduced a new API specific to time including the package java.time.* and calendar classes including LocalDateTime, ZonedDateTime, LocalDate, LocalTime, Period, etc. It also provides the class java.time.format.DateTimeFormatter used by the calendar classes to format date/time objects for output. It also parses the input strings and converts them to date/time objects.

 

Also, the objects of DateTimeFormatter are immutable i.e., once created the objects cannot be changed. The object can be used to create new objects, but the object itself won't change.

 

Formatting using Pre-Defined or In-Built Instances

 

The class DateTimeFormatter comes with pre-defined date and time formats following the ISO and RFC standards. This section provides the details to utilize the existing date and time formats provided by the DateTimeFormatter class. The default calendar system followed by these classes is ISO-8601.

 

We can use the ISO_LOCAL_DATE pre-defined instance to convert the LocalDate object to the ISO format as shown below.

 

// Get The pre-defined or in-built Formatter object
DateTimeFormatter isoFormatter = DateTimeFormatter.ISO_LOCAL_DATE;

// Get Today's date
LocalDate today = LocalDate.now();

// Convert date to ISO
String todayInIso = isoFormatter.format( today );

// Print the date in ISO
System.out.println( "Today in ISO: " + todayInIso );

// Output
Today in ISO: 2021-04-15

 

Similarly, we can use the RFC_1123_DATE_TIME pre-defined instance to convert the LocalDateTime object to the RFC 1123 format as shown below. RFC-1123 stands for Requirement for Internet Hosts and published by the IETF.

 

// Get The pre-defined or in-built Formatter object
DateTimeFormatter rfc1123Formatter = DateTimeFormatter.RFC_1123_DATE_TIME;

// Get Date and Time
LocalDateTime now = LocalDateTime.now();

// Convert to RFC 1123
String nowInRFC1123 = rfc1123Formatter.format( now.atZone( ZoneId.of( "UTC" ) ) );

// Print in RFC 1123
System.out.println( "Now in RFC 1123: " + nowInRFC1123 );

// Output
Now in RFC 1123: Thu, 15 Apr 2021 21:51:25 GMT

 

We can also format the ZonedDateTime as shown below. We can directly use the ZonedDateTime object without specifying the zone as we did in the previous example.

 

// Get The pre-defined or in-built Formatter object
DateTimeFormatter rfc1123Formatter = DateTimeFormatter.RFC_1123_DATE_TIME;

// Get Date and Time
ZonedDateTime now = ZonedDateTime.now();

// Convert to RFC 1123
String nowInRFC1123 = rfc1123Formatter.format( now );

// Print in RFC 1123
System.out.println( "Now in RFC 1123: " + nowInRFC1123 );

// Output
Now in RFC 1123: Thu, 15 Apr 2021 22:08:47 +0530

 

Formatting using Custom Formatter

 

We can also create custom formatter objects by defining our own pattern as shown below.

 

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

// Get Date and Time
LocalDateTime now = LocalDateTime.now();

// Convert to RFC 1123
String nowInCustom = customFormatter.format( now.atZone( ZoneId.of( "UTC" ) ) );

// Print in RFC 1123
System.out.println( "Now in Custom Format: " + nowInCustom );

// Output
Now in Custom Format: 04/15/2021 - 10:04pm UTC

 

We can also format the LocalDate and LocalTime objects using the custom formatter as shown below.

 

// Get The pre-defined or in-built Formatter object
DateTimeFormatter customDateFormatter = DateTimeFormatter.ofPattern( "MM/dd/yyyy" );
DateTimeFormatter customTimeFormatter = DateTimeFormatter.ofPattern( "hh:mma" );

// Get Date
LocalDate today = LocalDate.now();
LocalTime now = LocalTime.now();

// Convert date to string
String todayStr = customDateFormatter.format( today );

// Convert time to string
String nowStr = customTimeFormatter.format( now );

// Print in RFC 1123
System.out.println( "Today is: " + todayStr + " At: " + nowStr );

// Output:
Today is: 04/15/2021 At: 10:22pm

 

Useful Patterns

 

The below-mentioned table lists some of the useful patterns with example.

 

PatternExample
yyyy-MM-dd2021-04-15
yyyy-MM-dd hh:mm:ss2021-04-15 10:47:28
dd-MM-yyyy15-04-2021
dd-MMM-yyyy15-July-2021
dd/MM/yyyy15/04/2021
E, MMM dd yyyyThu, Apr 15 2021
hh:mm a10:44 pm
EEEE, MMM dd, yyyy HH:mm:ss aThursday, Apr 15, 2021 22:45:59 pm

 

Summary

 

This tutorial provided examples to format the objects of ZonedDateTime, LocalDateTime, LocalDate, and LocalTime using the in-built and custom formatter objects of the DateTimeFormatter class. It also provided the list of commonly used patterns with examples.

Share this blog:

Profile picture for user bhagwatchouhan
bhagwatchouhan