新Date()和FileCreation日期之间的奇怪区别

sak*_*ana 1 java date file

我正在运行一些测试来证明一个概念,我只是编写了这个代码并发现了一个奇怪的情况:

public class Test {

public static void main(String[] args) {

    Date now = new Date();

    File file = new File("/root/batch-experiments/test.txt");

    try {
        file.createNewFile();
    } catch (IOException e) {
        System.out.println("cannot create file...");
    }

    System.out.println(MessageFormat.format("Checking File {0}! Last Modified time is {1}. Must be newer than {2}", file.getName(),
            file.lastModified(), now.getTime()));

    if (file.lastModified() >= now.getTime()) {
        //ignore...
    } else {
        System.out.println(MessageFormat.format("File {0} is out of date and was ignored.", file));
    }
}
Run Code Online (Sandbox Code Playgroud)

}

输出是:

Checking File test.txt! Last Modified time is 1,253,187,650,000. Must be newer than 1,253,187,650,496
File /root/batch-experiments/test.txt is out of date and was ignored.
Run Code Online (Sandbox Code Playgroud)

怎么可能?新日期时间之后不应该是文件修改时间吗?这发生在4/5次尝试中.

我在这里缺少什么?

有什么方法可以保证新的Date()比文件创建更早?

Omr*_*dan 5

文件系统日期粒度通常为一秒(取决于实际的文件系统,它也可能更糟).创建文件时,创建时间将四舍五入.