withZoneSameInstant Versus withZoneSameLocal

Convert time zones with withZoneSameInstant to keep the same moment, or withZoneSameLocal to keep the wall clock and knowingly change the instant.

JavaData Fetching

Code

snippet
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ZoneConverter {

    public static void main(String[] args) {
        ZonedDateTime tokyoMeeting = ZonedDateTime.of(
                LocalDateTime.of(2026, 11, 19, 14, 0), ZoneId.of("Asia/Tokyo"));

        // withZoneSameInstant:同一时刻换时区,墙钟数字随偏移平移
        ZonedDateTime utcInstant = tokyoMeeting.withZoneSameInstant(ZoneId.of("UTC"));
        System.out.println("sameInstant: " + utcInstant);

        // withZoneSameLocal:墙钟数字不变,时刻本身被改写(versus 对照)
        ZonedDateTime utcLocal = tokyoMeeting.withZoneSameLocal(ZoneId.of("UTC"));
        System.out.println("sameLocal:   " + utcLocal);

        // 两条转换相差的正是两个时区之间的偏移
        long diffHours = Math.abs(Duration.between(
                utcInstant.toInstant(), utcLocal.toInstant()).toHours());
        System.out.println("instant gap hours: " + diffHours);
    }
}

What the code does

Under the hood: the class ZoneConverter and the function main.

Caveats

  • Copy, run: the standard library answers every call in the file.

Applying it

The run emits 3 labeled sections — sameInstant:, sameLocal:, instant gap hours: — one per step.

Statements that matter

ZonedDateTime utcInstant = tokyoMeeting.withZoneSameInstant(ZoneId.of("UTC"));
ZonedDateTime utcLocal = tokyoMeeting.withZoneSameLocal(ZoneId.of("UTC"));

Interactive Tools for This Topic

FAQ

What does withZoneSameInstant Versus withZoneSameLocal print when run?
The script runs standalone; its 3 labeled outputs include `sameInstant:`, `sameLocal:`, `instant gap hours:`.