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

Unintentional use of new Date(year...) with absolute year

XMLWordPrintable

    • b03

        It's well known that Date constructors use 1900-based years, which is very error prone. Here are some apparently inadvertent uses of such constructors in jdk tests, that ought to be fixed. The standard idiom is

        new Date(YYYY - 1900, ...)

        making it clearer what is going on.

        diff --git a/test/jdk/java/text/Format/DateFormat/DateFormatRegression.java b/test/jdk/java/text/Format/DateFormat/DateFormatRegression.java
        --- a/test/jdk/java/text/Format/DateFormat/DateFormatRegression.java
        +++ b/test/jdk/java/text/Format/DateFormat/DateFormatRegression.java
        @@ -1067,7 +1067,7 @@
                 TimeZone.setDefault(TimeZone.getTimeZone("PST"));
                 SimpleDateFormat fmt = new SimpleDateFormat("yy/MM/dd hh:ss zzz", Locale.JAPAN);
                 @SuppressWarnings("deprecation")
        - String result = fmt.format(new Date(1999, 0, 1));
        + String result = fmt.format(new Date(1999 - 1900, 0, 1));
                 logln("format()=>" + result);
                 if (!result.endsWith("PST")) {
                     errln("FAIL: SimpleDataFormat.format() did not retrun PST");
        diff --git a/test/jdk/java/time/tck/java/time/format/TCKLocalizedPrinterParser.java b/test/jdk/java/time/tck/java/time/format/TCKLocalizedPrinterParser.java
        --- a/test/jdk/java/time/tck/java/time/format/TCKLocalizedPrinterParser.java
        +++ b/test/jdk/java/time/tck/java/time/format/TCKLocalizedPrinterParser.java
        @@ -180,7 +180,7 @@
             @Test(dataProvider="time")
             public void test_time_print(LocalTime time, FormatStyle timeStyle, int timeStyleOld, Locale locale) {
                 DateFormat old = DateFormat.getTimeInstance(timeStyleOld, locale);
        - Date oldDate = new Date(1970, 0, 0, time.getHour(), time.getMinute(), time.getSecond());
        + Date oldDate = new Date(1970 - 1900, 0, 0, time.getHour(), time.getMinute(), time.getSecond());
                 String text = old.format(oldDate);
         
                 DateTimeFormatter f = builder.appendLocalized(null, timeStyle).toFormatter(locale);
        @@ -192,7 +192,7 @@
             @Test(dataProvider="time")
             public void test_time_parse(LocalTime time, FormatStyle timeStyle, int timeStyleOld, Locale locale) {
                 DateFormat old = DateFormat.getTimeInstance(timeStyleOld, locale);
        - Date oldDate = new Date(1970, 0, 0, time.getHour(), time.getMinute(), time.getSecond());
        + Date oldDate = new Date(1970 - 1900, 0, 0, time.getHour(), time.getMinute(), time.getSecond());
                 String text = old.format(oldDate);
         
                 DateTimeFormatter f = builder.appendLocalized(null, timeStyle).toFormatter(locale);
        diff --git a/test/jdk/java/util/Calendar/FieldStateTest.java b/test/jdk/java/util/Calendar/FieldStateTest.java
        --- a/test/jdk/java/util/Calendar/FieldStateTest.java
        +++ b/test/jdk/java/util/Calendar/FieldStateTest.java
        @@ -147,7 +147,7 @@
                         + "Then, getTime and set week of year to 43.");
         
                 @SuppressWarnings("deprecation")
        - Date d = new Date(2003 - 1990, OCTOBER, 31);
        + Date d = new Date(2003 - 1900, OCTOBER, 31);
                 cal.setTime(d);
                 cal.set(DAY_OF_WEEK, SUNDAY);
                 cal.set(2003, OCTOBER, 31); // 2003/10/31 is Friday.

              martin Martin Buchholz
              martin Martin Buchholz
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: