Close

Java Date Time - ZonedDateTime.format() Examples

Java Date Time Java Java API 


Class:

java.time.ZonedDateTime

java.lang.Objectjava.lang.Objectjava.time.ZonedDateTimejava.time.ZonedDateTimejava.time.temporal.TemporalTemporaljava.time.chrono.ChronoZonedDateTimeChronoZonedDateTimejava.io.SerializableSerializableLogicBig

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);
}
}

Output

2017-05-01T15:55:00.873-07:00[America/Los_Angeles]
BASIC_ISO_DATE: 20170501-0700
ISO_LOCAL_TIME: 15:55:00.873
ISO_OFFSET_DATE_TIME: 2017-05-01T15:55:00.873-07:00
ISO_OFFSET_DATE: 2017-05-01-07:00
ISO_OFFSET_TIME: 15:55:00.873-07:00
ISO_TIME: 15:55:00.873-07:00
ISO_OFFSET_TIME: 15:55:00.873-07:00
ISO_DATE: 2017-05-01-07:00
ISO_DATE_TIME: 2017-05-01T15:55:00.873-07:00[America/Los_Angeles]
ISO_INSTANT: 2017-05-01T22:55:00.873Z
ISO_LOCAL_DATE: 2017-05-01
ISO_LOCAL_DATE_TIME: 2017-05-01T15:55:00.873
ISO_ORDINAL_DATE: 2017-121-07:00
ISO_WEEK_DATE: 2017-W18-1-07:00
ISO_ZONED_DATE_TIME: 2017-05-01T15:55:00.873-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);
}
}
}
}

Output

2017-05-01T15:55:02.938-07:00[America/Los_Angeles]
---- ofLocalizedDate ----
FULL: Monday, May 1, 2017
LONG: May 1, 2017
MEDIUM: May 1, 2017
SHORT: 5/1/17
---- ofLocalizedTime ----
FULL: 3:55:02 PM PDT
LONG: 3:55:02 PM PDT
MEDIUM: 3:55:02 PM
SHORT: 3:55 PM
---- ofLocalizedDateTime ----
FULL: Monday, May 1, 2017 3:55:02 PM PDT
LONG: May 1, 2017 3:55:02 PM PDT
MEDIUM: May 1, 2017 3:55:02 PM
SHORT: 5/1/17 3:55 PM
---- ofLocalizedDateTime combinations----
dateStyle:FULL | timeStyle:FULL | date: Monday, May 1, 2017 3:55:02 PM PDT
dateStyle:FULL | timeStyle:LONG | date: Monday, May 1, 2017 3:55:02 PM PDT
dateStyle:FULL | timeStyle:MEDIUM | date: Monday, May 1, 2017 3:55:02 PM
dateStyle:FULL | timeStyle:SHORT | date: Monday, May 1, 2017 3:55 PM
dateStyle:LONG | timeStyle:FULL | date: May 1, 2017 3:55:02 PM PDT
dateStyle:LONG | timeStyle:LONG | date: May 1, 2017 3:55:02 PM PDT
dateStyle:LONG | timeStyle:MEDIUM | date: May 1, 2017 3:55:02 PM
dateStyle:LONG | timeStyle:SHORT | date: May 1, 2017 3:55 PM
dateStyle:MEDIUM | timeStyle:FULL | date: May 1, 2017 3:55:02 PM PDT
dateStyle:MEDIUM | timeStyle:LONG | date: May 1, 2017 3:55:02 PM PDT
dateStyle:MEDIUM | timeStyle:MEDIUM | date: May 1, 2017 3:55:02 PM
dateStyle:MEDIUM | timeStyle:SHORT | date: May 1, 2017 3:55 PM
dateStyle:SHORT | timeStyle:FULL | date: 5/1/17 3:55:02 PM PDT
dateStyle:SHORT | timeStyle:LONG | date: 5/1/17 3:55:02 PM PDT
dateStyle:SHORT | timeStyle:MEDIUM | date: 5/1/17 3:55:02 PM
dateStyle:SHORT | timeStyle:SHORT | date: 5/1/17 3:55 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);
}
}

Output

2017-05-01T15:55:04.947-07:00[America/Los_Angeles]
yyyy-MM-dd hh:mm a X => 2017-05-01 03:55 PM -07
yy-MM-dd hh:mm E a x => 17-05-01 03:55 Mon PM -07
YYYY-MMM-dd hh:mm F a x => 2017-May-01 03:55 1 PM -07
YYYY-MMM-dd hh:mm F a z => 2017-May-01 03:55 1 PM PDT
YYYY-MMM-dd hh:mm F a Z => 2017-May-01 03:55 1 PM -0700
YYYY-MMM-dd hh:mm F a VV => 2017-May-01 03:55 1 PM America/Los_Angeles
YYYY-MMM-dd hh:mm F a O => 2017-May-01 03:55 1 PM GMT-7




See Also