Close

Java Date Time - ZonedDateTime.ofInstant() 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

Methods:

public static ZonedDateTime ofInstant(Instant instant,
                                      ZoneId zone)

Returns an instance of ZonedDateTime from an Instant and zone ID.


public static ZonedDateTime ofInstant(LocalDateTime localDateTime,
                                      ZoneOffset offset,
                                      ZoneId zone)

Returns an instance of ZonedDateTime from local date-time, an zone offset and zone ID. The combination of LocalDateTime and ZoneOffset, uniquely specifies an instant without ambiguity.

Examples


package com.logicbig.example.zoneddatetime;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class OfInstantExample {

public static void main(String... args) {
ZonedDateTime d = ZonedDateTime.ofInstant(Instant.parse("2007-12-03T10:15:30.00Z"),
ZoneId.systemDefault());
System.out.println(d);
}
}

Output

2007-12-03T04:15:30-06:00[America/Chicago]




package com.logicbig.example.zoneddatetime;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class OfInstantExample2 {

public static void main(String... args) {
ZonedDateTime d = ZonedDateTime.ofInstant(LocalDateTime.now(), ZoneOffset.ofHours(-5),
ZoneId.of("US/Central"));
System.out.println(d);
}
}

Output

2017-05-01T16:01:26.588-05:00[US/Central]




See Also