Java Date Time Java Java API
Class:
java.time.ZonedDateTime
Method:
public String format(DateTimeFormatter formatter)
Returns the formatted string as specified by the provided
DateTimeFormatter.
Examples
 package com.logicbig.example.zoneddatetime;
import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter;
public class FormatExample {
public static void main(String... args) { ZonedDateTime d = ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("America/Los_Angeles")); System.out.println(d);
parse(d, DateTimeFormatter.BASIC_ISO_DATE, "BASIC_ISO_DATE"); parse(d, DateTimeFormatter.ISO_LOCAL_TIME, "ISO_LOCAL_TIME"); parse(d, DateTimeFormatter.ISO_OFFSET_DATE_TIME, "ISO_OFFSET_DATE_TIME"); parse(d, DateTimeFormatter.ISO_OFFSET_DATE, "ISO_OFFSET_DATE"); parse(d, DateTimeFormatter.ISO_OFFSET_TIME, "ISO_OFFSET_TIME"); parse(d, DateTimeFormatter.ISO_TIME, "ISO_TIME"); parse(d, DateTimeFormatter.ISO_OFFSET_TIME, "ISO_OFFSET_TIME"); parse(d, DateTimeFormatter.ISO_DATE, "ISO_DATE"); parse(d, DateTimeFormatter.ISO_DATE_TIME, "ISO_DATE_TIME"); parse(d, DateTimeFormatter.ISO_INSTANT, "ISO_INSTANT"); parse(d, DateTimeFormatter.ISO_LOCAL_DATE, "ISO_LOCAL_DATE"); parse(d, DateTimeFormatter.ISO_LOCAL_DATE_TIME, "ISO_LOCAL_DATE_TIME"); parse(d, DateTimeFormatter.ISO_ORDINAL_DATE, "ISO_ORDINAL_DATE"); parse(d, DateTimeFormatter.ISO_WEEK_DATE, "ISO_WEEK_DATE"); parse(d, DateTimeFormatter.ISO_ZONED_DATE_TIME, "ISO_ZONED_DATE_TIME"); }
private static void parse(ZonedDateTime d, DateTimeFormatter formatter, String formatterName) {
String s = d.format(formatter); System.out.printf("%20s: %s%n", formatterName, s); } }
Output2025-10-28T20:46:29.030-07:00[America/Los_Angeles] BASIC_ISO_DATE: 20251028-0700 ISO_LOCAL_TIME: 20:46:29.03 ISO_OFFSET_DATE_TIME: 2025-10-28T20:46:29.03-07:00 ISO_OFFSET_DATE: 2025-10-28-07:00 ISO_OFFSET_TIME: 20:46:29.03-07:00 ISO_TIME: 20:46:29.03-07:00 ISO_OFFSET_TIME: 20:46:29.03-07:00 ISO_DATE: 2025-10-28-07:00 ISO_DATE_TIME: 2025-10-28T20:46:29.03-07:00[America/Los_Angeles] ISO_INSTANT: 2025-10-29T03:46:29.030Z ISO_LOCAL_DATE: 2025-10-28 ISO_LOCAL_DATE_TIME: 2025-10-28T20:46:29.03 ISO_ORDINAL_DATE: 2025-301-07:00 ISO_WEEK_DATE: 2025-W44-2-07:00 ISO_ZONED_DATE_TIME: 2025-10-28T20:46:29.03-07:00[America/Los_Angeles]
 package com.logicbig.example.zoneddatetime;
import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle;
public class FormatExample2 {
public static void main(String... args) { ZonedDateTime d = ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("America/Los_Angeles")); System.out.println(d);
System.out.println("---- ofLocalizedDate ----"); for (FormatStyle formatStyle : FormatStyle.values()) { String s = d.format(DateTimeFormatter.ofLocalizedDate(formatStyle)); System.out.printf("%8s: %s%n", formatStyle.name(), s); }
System.out.println("---- ofLocalizedTime ----"); for (FormatStyle formatStyle : FormatStyle.values()) {
String s = d.format(DateTimeFormatter.ofLocalizedTime(formatStyle)); System.out.printf("%8s: %s%n", formatStyle.name(), s); }
System.out.println("---- ofLocalizedDateTime ----"); for (FormatStyle formatStyle : FormatStyle.values()) {
String s = d.format(DateTimeFormatter.ofLocalizedDateTime(formatStyle)); System.out.printf("%8s: %s%n", formatStyle.name(), s); }
System.out.println("---- ofLocalizedDateTime combinations----"); for (FormatStyle dateStyle : FormatStyle.values()) { for (FormatStyle timeStyle : FormatStyle.values()) { String s = d.format(DateTimeFormatter.ofLocalizedDateTime(dateStyle, timeStyle)); System.out.printf("dateStyle:%-8s| timeStyle:%-8s| date: %s%n", dateStyle.name(), timeStyle.name(), s); } } } }
Output2025-10-28T20:46:32.449-07:00[America/Los_Angeles] ---- ofLocalizedDate ---- FULL: Tuesday, October 28, 2025 LONG: October 28, 2025 MEDIUM: Oct 28, 2025 SHORT: 10/28/25 ---- ofLocalizedTime ---- FULL: 8:46:32 PM PDT LONG: 8:46:32 PM PDT MEDIUM: 8:46:32 PM SHORT: 8:46 PM ---- ofLocalizedDateTime ---- FULL: Tuesday, October 28, 2025 8:46:32 PM PDT LONG: October 28, 2025 8:46:32 PM PDT MEDIUM: Oct 28, 2025 8:46:32 PM SHORT: 10/28/25 8:46 PM ---- ofLocalizedDateTime combinations---- dateStyle:FULL | timeStyle:FULL | date: Tuesday, October 28, 2025 8:46:32 PM PDT dateStyle:FULL | timeStyle:LONG | date: Tuesday, October 28, 2025 8:46:32 PM PDT dateStyle:FULL | timeStyle:MEDIUM | date: Tuesday, October 28, 2025 8:46:32 PM dateStyle:FULL | timeStyle:SHORT | date: Tuesday, October 28, 2025 8:46 PM dateStyle:LONG | timeStyle:FULL | date: October 28, 2025 8:46:32 PM PDT dateStyle:LONG | timeStyle:LONG | date: October 28, 2025 8:46:32 PM PDT dateStyle:LONG | timeStyle:MEDIUM | date: October 28, 2025 8:46:32 PM dateStyle:LONG | timeStyle:SHORT | date: October 28, 2025 8:46 PM dateStyle:MEDIUM | timeStyle:FULL | date: Oct 28, 2025 8:46:32 PM PDT dateStyle:MEDIUM | timeStyle:LONG | date: Oct 28, 2025 8:46:32 PM PDT dateStyle:MEDIUM | timeStyle:MEDIUM | date: Oct 28, 2025 8:46:32 PM dateStyle:MEDIUM | timeStyle:SHORT | date: Oct 28, 2025 8:46 PM dateStyle:SHORT | timeStyle:FULL | date: 10/28/25 8:46:32 PM PDT dateStyle:SHORT | timeStyle:LONG | date: 10/28/25 8:46:32 PM PDT dateStyle:SHORT | timeStyle:MEDIUM | date: 10/28/25 8:46:32 PM dateStyle:SHORT | timeStyle:SHORT | date: 10/28/25 8:46 PM
 package com.logicbig.example.zoneddatetime;
import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter;
public class FormatExample3 {
public static void main(String... args) { ZonedDateTime d = ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("America/Los_Angeles")); System.out.println(d);
format(d, "yyyy-MM-dd hh:mm a X"); format(d, "yy-MM-dd hh:mm E a x"); format(d, "YYYY-MMM-dd hh:mm F a x"); format(d, "YYYY-MMM-dd hh:mm F a z"); format(d, "YYYY-MMM-dd hh:mm F a Z"); format(d, "YYYY-MMM-dd hh:mm F a VV"); format(d, "YYYY-MMM-dd hh:mm F a O");
}
private static void format(ZonedDateTime d, String pattern) { String s = d.format(DateTimeFormatter.ofPattern(pattern)); System.out.printf("%25s => %s%n", pattern, s); } }
Output2025-10-28T20:46:35.502-07:00[America/Los_Angeles] yyyy-MM-dd hh:mm a X => 2025-10-28 08:46 PM -07 yy-MM-dd hh:mm E a x => 25-10-28 08:46 Tue PM -07 YYYY-MMM-dd hh:mm F a x => 2025-Oct-28 08:46 7 PM -07 YYYY-MMM-dd hh:mm F a z => 2025-Oct-28 08:46 7 PM PDT YYYY-MMM-dd hh:mm F a Z => 2025-Oct-28 08:46 7 PM -0700 YYYY-MMM-dd hh:mm F a VV => 2025-Oct-28 08:46 7 PM America/Los_Angeles YYYY-MMM-dd hh:mm F a O => 2025-Oct-28 08:46 7 PM GMT-7
|
|