Search code examples
javadatetimecalendardst

Get Time in London


How can I get the current local wall clock time (in number of millis since 1 Jan 1970) in London? Since my application can run on a server in any location, I think I need to use a TimeZone of "Europe/London". I also need to take Daylight Savings into account i.e. the application should add an hour during the "summer".

I would prefer to use the standard java.util libraries.

Is this correct?

TimeZone tz = TimeZone.getTimeZone("Europe/London") ;
Calendar cal = Calendar.getInstance(tz);
return cal.getTime().getTime() + tz.getDSTSavings();

Thanks


Solution

  • I'm not sure what this quantity represents, since the "number of millis since 1 Jan 1970" doesn't vary based on location or daylight saving. But, perhaps this calculation is useful to you:

    TimeZone london = TimeZone.getTimeZone("Europe/London");
    long now = System.currentTimeMillis();
    return now + london.getOffset(now);
    

    Most applications are better served using either UTC time or local time; this is really neither. You can get the UTC time and time in a particular zone like this:

    Instant now = Instant.now(); /* UTC time */
    ZonedDateTime local = now.atZone(ZoneId.of("Europe/London"));