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. Creating DateTimeFormatter
1.1. Simple Formatters
We can create a very simple DateTimeFormatter
instance in 3 ways:
- Using predefined constants, such as
ISO_LOCAL_DATE
- Using localized styles with
FormatStyle
, such as LONG or MEDIUM - Creating custom patterns using pattern letters and
ofPattern()
method
//Use inbuilt pattern constants
DateTimeFormatter inBuiltFormatter1 = DateTimeFormatter.ISO_DATE_TIME;
DateTimeFormatter inBuiltFormatter2 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//Using FormatStyle
DateTimeFormatter customFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
//Define your own custom patterns
DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mma z");
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
2.1. Syntax
The DateTimeFormatter class provides mainly one method to format a given date-time instance of any type. It formats the dateTimeObject instance using the specified formatted.
String formmatedString = dateTimeObject.format(formatter);
2.2. Format 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. Format 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. Format 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. Format 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 Custom Patterns
Pattern | Example |
---|---|
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 !!
Leave a Reply