Java SE API

ZonedDateTime.withZoneSameInstant()

Change the zone without changing the instant. Java SE API; example uses Java 21-compatible syntax.

Formal Syntax & Structure

java syntaxjava
ZonedDateTime withZoneSameInstant(ZoneId zone)

Parameters & Attributes

NameTypeDescription
zoneZoneIdTarget zone

Return Value

ZonedDateTime

Change the zone without changing the instant.

Working Example

Example Snippetjava
import java.math.*;import java.time.*;import java.time.format.*;import java.time.temporal.*;import java.text.*;import java.util.*;public class Main {    public static void main(String[] args) throws Exception {        Instant event = Instant.parse("2024-01-01T00:30:00Z");        var stockholm = event.atZone(ZoneId.of("Europe/Stockholm"));        var newYork = stockholm.withZoneSameInstant(ZoneId.of("America/New_York"));        System.out.println(stockholm.toLocalDate());        System.out.println(newYork.toLocalDate());        System.out.println(stockholm.toLocalTime());        System.out.println(stockholm.toInstant().equals(newYork.toInstant()));    }}
Java Reference