In this article, we will learn how to format ZonedDateTime in different Format Style provided in Java 1.8 version
1. Format ZonedDateTime in different Format Style :
- FormatStyle class provides 4 different enum constants for formatting ZonedDateTime, those are
- FormatStyle.SHORT
- FormatStyle.MEDIUM
- FormatStyle.LONG
- FormatStyle.FULL
- In below illustration, we are using above provided in-built formats to format ZonedDateTime as listed below,
- DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
- DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
- DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
- DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
- Note: for creating above formats we need DateTimeFormatter
2. ZonedDateTime examples in different Format Style :
2.1 ZonedDateTime to FormatStyle.SHORT format :
- This format style converts ZonedDateTime in default (yyyy-MM-ddTHH:mm:ss.nnn+HH:mm[Region/City]) format to (dd/MM/yy, hh:mm a) format ignoring second & nanosecond fields
FormatZonedDateTimeToShortStyle.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class FormatZonedDateTimeToShortStyle {
public static void main(String[] args) {
// 1. get Zoned Date/time
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("Zoned Date/time is :- \n" + zonedDateTime);
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.SHORT);
// 3. ZonedDateTime to FormatStyle.SHORT format in String-form
String str = zonedDateTime.format(dateTimeFormatter);
System.out.print("\nZonedDateTime to FormatStyle.SHORT format :- \n" + str);
}
}
Output:
Zoned Date/time is :-
2022-08-13T01:40:47.739222100+05:30[Asia/Calcutta]
ZonedDateTime to FormatStyle.SHORT format :-
13/08/22, 1:40 am
2.2 ZonedDateTime to FormatStyle.MEDIUM format :
- This format style converts ZonedDateTime in default (yyyy-MM-ddTHH:mm:ss.nnn+HH:mm[Region/City]) format to (dd-MMM-yyyy, hh:mm:ss a) format ignoring nanosecond field
FormatZonedDateTimeToMediumStyle.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class FormatZonedDateTimeToMediumStyle {
public static void main(String[] args) {
// 1. get Zoned Date/time
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("Zoned Date/time is :- \n" + zonedDateTime);
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.MEDIUM);
// 3. ZonedDateTime to FormatStyle.MEDIUM format in String-form
String str = zonedDateTime.format(dateTimeFormatter);
System.out.print("\nZonedDateTime to FormatStyle.MEDIUM format :- \n" + str);
}
}
Output:
Zoned Date/time is :-
2022-08-13T01:41:09.109723600+05:30[Asia/Calcutta]
ZonedDateTime to FormatStyle.MEDIUM format :-
13-Aug-2022, 1:41:09 am
2.3 ZonedDateTime to FormatStyle.LONG format :
- This format style converts ZonedDateTime in default (yyyy-MM-ddTHH:mm:ss.nnn+HH:mm[Region/City]) format to (dd MMMM yyyy at hh:mm:ss a z) format ignoring nanosecond field
FormatZonedDateTimeToLongStyle.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class FormatZonedDateTimeToLongStyle {
public static void main(String[] args) {
// 1. get Zoned Date/time
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("Zoned Date/time is :- \n" + zonedDateTime);
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.LONG);
// 3. ZonedDateTime to FormatStyle.LONG format in String-form
String str = zonedDateTime.format(dateTimeFormatter);
System.out.print("\nZonedDateTime to FormatStyle.LONG format :- \n" + str);
}
}
Output:
Zoned Date/time is :-
2022-08-13T01:42:01.991101800+05:30[Asia/Calcutta]
ZonedDateTime to FormatStyle.LONG format :-
13 August 2022 at 1:42:01 am IST
2.4 ZonedDateTime to FormatStyle.FULL format :
- This format style converts ZonedDateTime in default (yyyy-MM-ddTHH:mm:ss.nnn+HH:mm[Region/City]) format to (EEEE, dd MMMM, yyyy at hh:mm:ss a z) format ignoring nanosecond field
FormatZonedDateTimeToFullStyle.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class FormatZonedDateTimeToFullStyle {
public static void main(String[] args) {
// 1. get Zoned Date/time
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("Zoned Date/time is :- \n" + zonedDateTime);
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.FULL);
// 3. ZonedDateTime to FormatStyle.FULL format in String-form
String str = zonedDateTime.format(dateTimeFormatter);
System.out.print("\nZonedDateTime to FormatStyle.FULL format :- \n" + str);
}
}
Output:
Zoned Date/time is :-
2022-08-13T01:42:57.270655200+05:30[Asia/Calcutta]
ZonedDateTime to FormatStyle.FULL format :-
Saturday, 13 August, 2022 at 1:42:57 am India Standard Time
Related Articles:
- Java 8 – ZonedDateTime with method details and examples
- Java 8 – How to get Date, Time and Zone fields from ZonedDateTime ?
- Java 8 – How to form ZonedDateTime passing Date, Time and Zone fields ?
- Java 8 – How to form ZonedDateTime passing LocalDate, LocalTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing LocalDateTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing Instant and ZoneId ?
- Java 8 – How to parse ZonedDateTime in String form ?
- Java 8 – How to convert String to ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to String ?
- Java 8 – How to convert ZonedDateTime in different formats ?
- Java 8 – How to convert ZonedDateTime in different Format Style ?
- Java 8 – How to convert ZonedDateTime to LocalDateTime ?
- Java 8 – How to convert ZonedDateTime to an OffsetDateTime ?
- Java 8 – How to convert ZonedDateTime to an Instant ?
- Java 8 – How to extract LocalDate and LocalTime and LocalDateTime from ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert ZonedDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert ZonedDateTime to Calendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to ZonedDateTime in different ways ?
- Java 8 – How to add Date and Time fields to ZonedDateTime ?
- Java 8 – How to subtract Date and Time fields from ZonedDateTime ?
- Java 8 – How to alter Date, Time and Zone fields of ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is Before another ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is After another ZonedDateTime ?
- Java 8 – How to compare two ZonedDateTime instances ?
- Java 8 – How to find difference between two ZonedDateTime using Period & Duration ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
- https://docs.oracle.com/javase/8/docs/api/java/time/format/FormatStyle.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!