代码之家  ›  专栏  ›  技术社区  ›  sakana

新日期()和文件创建日期之间的奇怪差异

  •  1
  • sakana  · 技术社区  · 15 年前

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

    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));
        }
    }
    

    }

    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.
    

    这怎么可能? 文件修改时间不应该晚于新日期时间吗? 这种情况每4/5次就发生1次。

    4 回复  |  直到 15 年前
        1
  •  5
  •   Omry Yadan    15 年前

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

        2
  •  2
  •   Zed    15 年前

    上次修改的粒度可能小于毫秒。

        3
  •  2
  •   KLE rslite    15 年前

    在我们的机器上。具体地说,在我们的日志模块中,我们看到日期之间的差值是:15毫秒、16毫秒、31毫秒、47毫秒。。。


    最新的检查器,其公差约为300毫秒至2秒 (对于远程文件,还有更多)。因此,他们认为,依赖文件是最新的,如果它是后部,或前部(但不超过300毫秒)。

        4
  •  0
  •   Gothmog    15 年前