--- old/src/java.base/share/classes/java/io/File.java 2017-05-22 13:15:56.000000000 -0700 +++ new/src/java.base/share/classes/java/io/File.java 2017-05-22 13:15:56.000000000 -0700 @@ -934,7 +934,9 @@ * (00:00:00 GMT, January 1, 1970), or 0L if the * file does not exist or if an I/O error occurs; the value may * be negative in which case its absolute value indicates the - * number of milliseconds before the epoch + * number of milliseconds before the epoch; if the last-modified + * time is the epoch, the value will be unity (1) + * so as to avoid ambiguity with the case of a non-existent file * * @throws SecurityException * If a security manager exists and its {@link @@ -949,7 +951,18 @@ if (isInvalid()) { return 0L; } - return fs.getLastModifiedTime(this); + long lastModifiedTime = fs.getLastModifiedTime(this); + + // + // In the infinitesimally probable event that the last-modified time + // of the file is the epoch (zero), return unity instead so as to avoid + // ambiguity with the case of a non-existent file which returns zero. + // + if (lastModifiedTime == 0) { + lastModifiedTime = 1; + } + + return lastModifiedTime; } /** --- /dev/null 2017-05-22 13:15:57.000000000 -0700 +++ new/test/java/io/File/FileTimeAtEpoch.java 2017-05-22 13:15:57.000000000 -0700 @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8180767 + * @summary Ensure that lastModified() returns unity for files modified at epoch + */ + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.attribute.FileTime; +import java.util.concurrent.TimeUnit; + +public class FileTimeAtEpoch { + public static void main(String[] args) throws Throwable { + String dirname = System.getProperty("test.dir", "."); + System.out.format("Working directory: %s%n", dirname); + File dir = new File(dirname); + File file = File.createTempFile("epoch", ".txt"); + System.out.format("Test file: %s%n", file.getAbsolutePath()); + FileTime epoch = FileTime.from(0L, TimeUnit.MILLISECONDS); + Path path = file.toPath(); + FileTime current = Files.getLastModifiedTime(path); + System.out.format("Current last-modified time: %s%n", + current.toString()); + Files.setLastModifiedTime(path, epoch); + FileTime updated = Files.getLastModifiedTime(path); + System.out.format("Updated last-modified time: %s%n", + updated.toString()); + long lastModified = file.lastModified(); + if (lastModified != 1L) { + throw new RuntimeException + ("Expected last-modified time of 1 but obtained " + + lastModified); + } + } +}