Codectionary / Developer documentation / Java

Time Zone Conversion

ZonedDateTime combines a local date/time with zone rules. withZoneSameInstant changes the displayed clock time while preserving the actual instant. Save as Main.java and run with a Java 17+ JDK: java Main.java.

Syntax

value.withZoneSameInstant(ZoneId.of("UTC"))

Examples

Time Zone Conversion

Save as Main.java and run with a Java 17+ JDK: java Main.java.

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

public class Main {
  public static void main(String[] args) throws Exception {
    ZonedDateTime meeting = ZonedDateTime.parse("2024-07-01T09:00:00+01:00[Europe/London]");
    ZonedDateTime utc = meeting.withZoneSameInstant(ZoneId.of("UTC"));
    System.out.println(utc.toLocalTime());
    System.out.println(meeting.toInstant().equals(utc.toInstant()));
  }
}
  1. Expected output: 08:00 true
  2. London uses the +01:00 offset on this date. Converting to UTC subtracts an hour from the displayed time, not from the instant.

Best practices

  • Use region IDs when daylight-saving rules matter; offsets alone do not encode those rules.
  • Define a policy for ambiguous or nonexistent local times.

Runtime & versions

Java Virtual Machine. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Java Language Specification

Specifications & further reading