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

ZonedDateTime/OffsetDateTime.toString return invalid ISO-8601 for years <= 1893

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      b24
    • CPU:
      generic
    • OS:
      generic

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Tested with JDK 1.8.0_66 11.0.5 11.0.4 17.0.1 18-ea, on openjdk:18 docker image, on linux and windows

      A DESCRIPTION OF THE PROBLEM :
      The following code prints out the string 1893-01-01T00:00+02:30:17. This should be ISO-8601, but this standard doesn't support seconds in the time offset. The time offset itself might be correct, considering there was probably no international time zone standard at that time, and this is probably the offset of the sun's noon location of this time zone city.

      But as an ISO-8601, this formatting is simply invalid and cannot be parsed for example by Angular applications.

      Code for reproducing:

          import java.time.*;

          ZoneId zone = ZoneId.of("Europe/Moscow");
          ZonedDateTime zonedDateTime = ZonedDateTime.of(1893, 1, 1, 0, 0, 0, 0, zone);
          System.out.println(zonedDateTime.toString()); // 1893-01-01T00:00+02:30:17[Europe/Moscow]

          OffsetDateTime offsetDateTime = zonedDateTime.toOffsetDateTime();
          System.out.println(offsetDateTime.toString()); // 1893-01-01T00:00+02:30:17

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the code below

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      1893-01-01T00:00+03:00
      1893-01-01T00:00+03:00

      or

      1893-01-01T00:00+02:30
      1893-01-01T00:00+02:30

      The time zone should conform to ISO-8601. I guess the time zone information itself is third-party, so there's no influence, but at least the seconds part should be cropped, or the toString method thould not be declared as ISO-8601 compilant (javadoc)
      ACTUAL -
      1893-01-01T00:00+02:30:17[Europe/Moscow]
      1893-01-01T00:00+02:30:17

      ---------- BEGIN SOURCE ----------
      Try the above code, or the complete version

      import java.time.OffsetDateTime;
      import java.time.ZoneId;
      import java.time.ZonedDateTime;

      public class Main {
        public static void main(String[] args) {
          ZoneId zone = ZoneId.of("Europe/Moscow");
          ZonedDateTime zonedDateTime = ZonedDateTime.of(1893, 1, 1, 0, 0, 0, 0, zone);
          System.out.println(zonedDateTime.toString()); // 1893-01-01T00:00+02:30:17[Europe/Moscow]

          OffsetDateTime offsetDateTime = zonedDateTime.toOffsetDateTime();
          System.out.println(offsetDateTime.toString()); // 1893-01-01T00:00+02:30:17
        }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
          if (dateTime.getYear() < 1900) {
            return dateTime.atOffset(ZoneOffset.ofHours(3));
          } else {
            return dateTime.atZone(ZoneId.systemDefault()).toOffsetDateTime();
          }

      FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

              Assignee:
              naoto Naoto Sato
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: