In this article, we will learn how to form ZonedDateTime passing Instant & Zone fields using ZonedDateTime.of() method provided in Java 1.8 version
Form ZonedDateTime passing Instant & Zone Fields :
- ZonedDateTime.of() method returns ZonedDateTime passing Instant and Zone (Zone/Offset) fields
- ZonedDateTime.of(Instant, ZoneId) –
- Obtains an instance of
ZonedDateTime
from anInstant
- Obtains an instance of
- Finally, print ZonedDateTime in different formatted style like SHORT, MEDIUM, LONG & FULL to the console
FormZonedDateTime4.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class FormZonedDateTime4 {
public static void main(String[] args) {
// 1. LocalDateTime
Instant instant = Instant.now();
System.out.println("Instant = \n" + instant);
// 2. ZoneId
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("\nZoneId :- \n" + zoneId);
// 3. form ZonedDateTime passing Instant & ZoneId
ZonedDateTime zonedDateTime = ZonedDateTime
.ofInstant(instant, zoneId);
System.out.println("\nZonedDateTime :- \n" + zonedDateTime);
// 3.1 format ZonedDateTime in FormatStyle.SHORT
String formattedStr1 = zonedDateTime.format(
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT));
System.out.println("\nZonedDateTime in FormatStyle.SHORT :- \n" + formattedStr1);
// 3.2 format ZonedDateTime in FormatStyle.MEDIUM
String formattedStr2 = zonedDateTime.format(
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
System.out.println("\nZonedDateTime in FormatStyle.MEDIUM :- \n" + formattedStr2);
// 3.3 format ZonedDateTime in FormatStyle.LONG
String formattedStr3 = zonedDateTime.format(
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG));
System.out.println("\nZonedDateTime in FormatStyle.LONG :- \n" + formattedStr3);
// 3.4 format ZonedDateTime in FormatStyle.FULL
String formattedStr4 = zonedDateTime.format(
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL));
System.out.print("\nZonedDateTime in FormatStyle.FULL :- \n" + formattedStr4);
}
}
Output:
Instant =
2022-08-12T04:15:05.806136300Z
ZoneId :-
Asia/Calcutta
ZonedDateTime :-
2022-08-12T09:45:05.806136300+05:30[Asia/Calcutta]
ZonedDateTime in FormatStyle.SHORT :-
12/08/22, 9:45 am
ZonedDateTime in FormatStyle.MEDIUM :-
12-Aug-2022, 9:45:05 am
ZonedDateTime in FormatStyle.LONG :-
12 August 2022 at 9:45:05 am IST
ZonedDateTime in FormatStyle.FULL :-
Friday, 12 August, 2022 at 9:45:05 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/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Month.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 !!