首页javafile_attributeJava I/O - 如何比较文件日期

Java I/O - 如何比较文件日期

我们想知道如何比较文件日期。
import java.io.File;

public class Main {
  public static void main(String[] args) {
    String f1 = "run.bat";
    long d1 = new File(f1).lastModified();

    String f2 = "build.xml";
    long d2 = new File(f2).lastModified();

    String relation;
    if (d1 == d2)
      relation = "the same age as";
    else if (d1 < d2)
      relation = "older than";
    else
      relation = "newer than";
    System.out.println(f1 + " is " + relation + ' ' + f2);
  }
}