Compare File Dates in Java

Description

The following code shows how to compare File Dates.

Example


/*  w w  w . j  a v  a 2  s .  co m*/
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);
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip