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

Date.fromInstant(Instant) gives incorrect results for older instant values

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      When calling Date.fromInstant(Instant) for instants pointing to a date before 1582-10-15, the conversion does simple millisecond calculation, probably ignoring the fact that leap years not counted before. I thought that the threshold date would be 1582-03-21 (when the date correction was applied and leap years introduced), still the correct calculation gets applied from 15th of October.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute Date.fromInstant(Instant) for instants before 1582-10-15, at 1582-10-15 and later.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Resulting date should match the input instant.
      ACTUAL -
      Resulting date is earlier for instants before 1582-10-15.

      ---------- BEGIN SOURCE ----------
      import static org.junit.Assert.assertEquals;

      import java.text.DateFormat;
      import java.text.SimpleDateFormat;
      import java.time.Instant;
      import java.time.LocalDateTime;
      import java.time.ZoneId;
      import java.time.format.DateTimeFormatter;
      import java.util.Arrays;
      import java.util.Date;
      import java.util.List;

      import org.junit.Test;
      import org.junit.runner.RunWith;
      import org.junit.runners.Parameterized;
      import org.junit.runners.Parameterized.Parameters;

      @RunWith(Parameterized.class)
      public class DateTest {

          private static final String DATE_PREFIX = "1582-10-";
          private static final String DATE_SUFFIX = "T00:00";

          private final ZoneId zoneId = ZoneId.of("UTC");
          private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

          private final int day;

          public DateTest(int day) {
              this.day = day;
          }

          @Parameters(name = "{index} -> day: {0}")
          public static List<Integer> parameters() {
              return Arrays.asList(13, 14, 15, 16, 17);
          }

          @Test
          public void testDateFromInstant() {
              String dayString = Integer.toString(day);
              while (dayString.length() < 2) {
                  dayString = "0" + dayString;
              }

              String dateString = DATE_PREFIX + dayString;
              String dateTimeString = dateString + DATE_SUFFIX;

              LocalDateTime localDateTime = LocalDateTime.parse(dateTimeString, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
              Instant instant = localDateTime.atZone(zoneId).toInstant();
              Date date = Date.from(instant);
              String formatedDate = dateFormat.format(date);
              assertEquals(dateString + " != " + formatedDate, dateString, formatedDate);
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


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

              Created:
              Updated:
              Resolved: