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

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

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

    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 回复  |  直到 17 年前
        1
  •  5
  •   Omry Yadan    17 年前

        2
  •  2
  •   Zed    17 年前

        3
  •  2
  •   KLE rslite    17 年前

    日期对象给出毫秒数,但仅每15.5毫秒一次 在我们的机器上。具体来说,在我们的日志模块中,我们看到日期之间的增量为:15ms、16ms、31ms、47ms。..


    最新的检查器,容差约为300ms至2s

        4
  •  0
  •   Gothmog    17 年前

    也许编译器重新排序了指令的执行方式?