Bug in File.getLastModified() (original) (raw)

Ricardo Almeida ric.almeida at gmail.com
Wed Mar 29 14:40:01 UTC 2017


Hi,

I could not raise a bug in https://bugs.openjdk.java.net/ so I hope it is ok to say it here, hoping someone can take a look at it...

File.getLastModified() always returns with second precision, losing the miliseconds (i.e. a number ending in 000). I tried using Files.getLastModifiedTime and it seems to work correctly as the output of the following test app shows:

Test f.lastModified [1490606336000]: false Test Files.getLastModifiedTime [1490606336718]: true

The code:

package com.espatial.test;

import java.io.File; import java.io.IOException; import java.nio.file.Files;

public class FileTest { private static final long LM = 1490606336718L;

public static void main(String[] args) throws IOException {
    File f = new File("test.txt");
    f.createNewFile();

    f.setLastModified(LM);

    System.out.printf("Test f.lastModified [%s]: %b\n",

f.lastModified(), f.lastModified() == LM); System.out.printf("Test Files.getLastModifiedTime [%s]: %b\n", Files.getLastModifiedTime(f.toPath()).toMillis(), (Files.getLastModifiedTime(f.toPath()).toMillis() == LM));

    f.delete();
}

}

Thanks you, Regards, Ricardo Almeida



More information about the build-dev mailing list