Java Data Type How to - Change date time in a timezone by offset








Question

We would like to know how to change date time in a timezone by offset.

Answer

import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
/* w w w .  j  a  v  a 2 s. com*/
public class Main {

  public static void main(String[] args) {
    ZonedDateTime nowInAthens = ZonedDateTime.now(ZoneId.of("Europe/Athens"));

    System.out.println(nowInAthens.withZoneSameInstant(ZoneId.ofOffset("UTC",
        ZoneOffset.ofHours(3))));
  }
}

The code above generates the following result.