Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8175709

DateTimeFormatterBuilder.appendZoneId() has misleading JavaDoc

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 25
    • None
    • core-libs
    • master

      (filed on behalf of a colleague)

      DateTimeFormatterBuilder.appendZoneId() (and all the similar, related methods) suggests that parsing "+01:00", "UT+01:00", "UTC+01:00" or "GMT+01:00" will result in some ZoneOffset being returned.

      However, of those only "+01:00" actually returns a ZoneOffset, all the others return a ZoneId (with the normalized input text in it, basically). Which matches the fact that only ZoneOffset.of("+01:00") actually returns a ZoneOffset, all other calls (with "UT", "UTC" or "GMT" prefix) actually throw a DateTimeException.

      Parsing "GMT+01:00" (or any other variant with a prefix) will return a ZoneId with the respective value in it.

      The ZoneOffset.of("UTC+01:30") mentioned in the JavaDoc of appendZoneOrOffsetId (only) does not actually return a ZoneOffset, but throws an exception instead.

      In its place other methods JavaDoc mention ZoneOffset.of("+01:30"), which does work. They do not, however actually return a ZoneOffset in every case where they claim they do (specifically they only return it for input that looks like "+01:30", i.e. has no prefix).

      Here is a repro:

      public class DateTimeFormatter {
          static void printZoneId(String input) {
              java.time.ZoneId zoneId =
                  new java.time.format.DateTimeFormatterBuilder()
                  .appendZoneOrOffsetId()
                  .toFormatter(java.util.Locale.ROOT)
                  .parse(input)
                  .query(java.time.temporal.TemporalQueries.zoneId());
              System.out.printf("\"%s\" => zoneId=%s class=%s\n",
                                input, zoneId, zoneId.getClass().getSimpleName());
          }

          public static void main(String[] args) throws Throwable {
              printZoneId("+01:30");
              printZoneId("UT+01:30");
              printZoneId("UTC+01:30");
              printZoneId("GMT+01:30");
          }
      }

      "+01:30" => zoneId=+01:30 class=ZoneOffset
      "UT+01:30" => zoneId=UT+01:30 class=ZoneRegion
      "UTC+01:30" => zoneId=UTC+01:30 class=ZoneRegion
      "GMT+01:30" => zoneId=GMT+01:30 class=ZoneRegion

            naoto Naoto Sato
            martin Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: