javautciso8601gmt

Understanding specific UTC time format YYYY-MM-DDTHH:MM:SS.SSSZ


I have two related questions.

Assume a program running in (British Standard Time)BST generates a date time value for current time in UTC (YYYY-MM-DDTHH:MM:SS.SSSZ) format.

Also assume current time in London is 2016-06-01 12:33:54.

  1. If the current time given by the program is 2016-06-01T11:33:54.000Z , is the program wrong?

  2. How is summertime offset for BST noted in the corresponding time format for YYYY-MM-DDTHH:MM:SS.SSSZ

I assume YYYY-MM-DDTHH:MM:SS+0001. Am I correct ?


Solution

  • Firstly please have a read of the iso8601 information. It's becoming more common place to deal with times in different time zones (e.g. server time zone and client time zone) and the standard is really useful.

    In particular please read about UTC or "Zulu" time here.

    1. The program is correct, since the London time is one hour ahead of "UTC" time in summer

    2. The trailing 'Z' is a short notation for UTC (Zulu). You could also write "+00:00" instead of 'Z'. The SS.SSS refer to seconds and milliseconds - not related to the time zone. In devnull's comment he shows you how to apply an offset for summer.

    Edit:

    There's been some discussion in the comments about whether iso8601 timezone includes timezone or not, and whether timezone will in fact be printed out.

    This depends completely on the date/time implementation. If we are using SimpleDateFormat then timezone is supported and will be printed.

    Here's a code example to illustrate

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    System.out.println(formatter.format(new Date()));
    formatter.setTimeZone(TimeZone.getTimeZone("Europe/London"));
    System.out.println(formatter.format(new Date()));
    

    Output

    2016-06-02T12:53:14.924Z
    2016-06-02T13:53:14.925+01:00
    

    Naturally, if you are using a different date/time library such as joda-time, then the implentation details will be different.

    Edit: As @DerrylThomas pointed out with SimpleDateFormat wise to use lower case y for years - unless it's intended to use week year - explained in a bit of detail in another answer to a similar question https://stackoverflow.com/a/56911450.