JDK-8062024 : Issue with date.setFullYear when time other than midnight
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Affected Version: 8u25,9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2014-10-22
  • Updated: 2015-06-04
  • Resolved: 2014-10-27
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u40Fixed 9 b37Fixed
Description
FULL PRODUCT VERSION :
java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
There is a discrepancy between the Java 8 Nashorn implementation of setFullYear and other implementations.  If the time is set on a Date object when calling setFullYear the new Date is calculated to be one day less than if the date is set to midnight (00:00:00:00) and is also one day less than the Rhino engine used in Java 7.

REGRESSION.  Last worked in version 7u45

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create a date with the time set e.g. new Date("January 01, 1950 00:00:01");
call setFullYear(1960) on the date
the date is now:  Thu Dec 31 1959 00:00:01 GMT+0000 (GMT) rather than 
Fri Jan 01 1960 00:00:01 GMT-0000 (GMT)

This was noticed in regression testing of moving some code from Java 7 to Java 8. (Rhino returns the expected result, Nashorn returns the actual).


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Before: Sun Jan 01 1950 00:00:00 GMT-0000 (GMT)
After: Fri Jan 01 1960 00:00:00 GMT-0000 (GMT)
Before: Sun Jan 01 1950 00:00:01 GMT-0000 (GMT)
After: Fri Jan 01 1960 00:00:01 GMT-0000 (GMT)

This was produced using Java 7 Rhino.

ACTUAL -
Before: Sun Jan 01 1950 00:00:00 GMT+0000 (GMT)
After: Fri Jan 01 1960 00:00:00 GMT+0000 (GMT)
Before: Sun Jan 01 1950 00:00:01 GMT+0000 (GMT)
After: Thu Dec 31 1959 00:00:01 GMT+0000 (GMT)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
print("version: " +java.lang.System.getProperty("java.version"));
var date1 = new Date("January 01, 1950 00:00:00");
print("Before: "+date1);
date1.setFullYear(1960);
print("After: "+date1);

var date2 = new Date("January 01, 1950 00:00:01");
print("Before: "+date2);
date2.setFullYear(1960);
print("After: "+date2);
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
The time element was actually unnecessary for our usage so we were able to amend the code to set the date to midnight before calling setFullYear()




Comments
Nashorn engine in openjdk 8+ is not a compatible replacement for Rhino engine in jdk 6 and jdk 7. It is a clean-room implementation that complies to ECMAScript 262 Edition 5.1 standard (+ few extensions). This issue has to be investigated and fixed, if needed. But, this is not a regression by definition.
27-10-2014