Java Data Type How to - Get current time in Europe/Berlin








Question

We would like to know how to get current time in Europe/Berlin.

Answer

import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
/*w ww  .  ja va 2  s .  co  m*/
public class Main {

  public static void main(String[] args) {
    System.out.println(getCurrentTimeStamp());
  }

  public static String getCurrentTimeStamp() {
    Clock clock = Clock.system(ZoneId.of("Europe/Berlin"));
    ZonedDateTime now = ZonedDateTime.now(clock);
    return DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(now);
  }
}

The code above generates the following result.