JDK-6791812 : (file spec) Incompatible File.lastModified() and setLastModified() for negative time
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 6u10
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2009-01-09
  • Updated: 2017-07-18
  • Resolved: 2017-06-13
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 10 JDK 9
10Fixed 9 b175Fixed
Related Reports
Duplicate :  
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
File.lastModified() returns a negative value when the timestamp is before the epoch. However, File.setLastModified() does not allow negative timestamps --- the specs require that it throws an IllegalArgumentException if the argument is negative. This behavior is incompatible.

JUSTIFICATION :
Violates our assumption on how the pair of methods should work --- a valid return value from lastModified() should be a valid argument for setLastModified(). This poses problems when synchronizing files.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
setLastModified() should accept negative values.
ACTUAL -
setLastModified() throws an IllegalArgumentException if the argument is negative.

---------- BEGIN SOURCE ----------
/* create the file "test" with a last modified timestamp that is before the epoch (i.e. 1970), e.g. by using touch */

public static void main(String[] args)
{
		File f = new File("test");
		long time = f.lastModified();
		f.setLastModified(time); /* throws an IllegalArgumentException */
}
---------- END SOURCE ----------

Comments
Appending a reference to Files.getLastModifiedTime as a second alternative would not hurt either.
26-05-2017

The new file system API (java.nio.file) supports file times before the epoch. For java.io.File then we should just clarify lastModified to make it clear that it may return a negative value.
25-10-2012