Convert current date and time to another time zone

Description

The following code shows how to convert current date and time to another time zone.

Example


import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
// w w  w  .ja va2s  . co  m
public class Main {
  public static void main(String[] args) {
    Set<String> allZones = ZoneId.getAvailableZoneIds();
    List<String> zoneList = new ArrayList<String>(allZones);
    Collections.sort(zoneList);

    LocalDateTime dt = LocalDateTime.now();
    for (String s : zoneList) {
      ZoneId zone = ZoneId.of(s);
      ZonedDateTime zdt = dt.atZone(zone);
      ZoneOffset offset = zdt.getOffset();
      String out = String.format("%35s %10s%n", zone, offset);
      System.out.println(out);
    }
  }
}

The code above generates the following result.





















Home »
  Java Date Time »
    Example »




Convert
Date
Timezone