Java examples to use DateTimeFormatter
for formatting ZonedDateTime
, LocalDateTime
, LocalDate
and LocalTime
to string with predefined and custom patterns.
DateTimeFormat is thread-safe and immutable.
1. Create DateTimeFormatter
We can create DateTimeFormatter
in two ways:
- Using inbuilt patterns
- Using custom patterns using
ofPattern()
method - 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);
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
3. Format LocalDateTime
LocalDate
does not have 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
4. 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
5. Format LocalTime
LocalTime
does not have 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
6. Useful formatting 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” |
7. More Examples
Happy Learning !!
References:
- DateTimeFormatter Javadoc
- ZonedDateTime Javadoc
- LocalDateTime Javadoc
- LocalDate Javadoc
- LocalTime Javadoc
Was this post helpful?
Let us know if you liked the post. That’s the only way we can improve.
Phlip
Why does String dateString = DateTimeFormatter.ofPattern(“MM/dd/yy”).format(time); give me “Unsupported field: MonthOfYear”? It looks exactly like your examples