I'm running some test to prove a concept and i just wrote this code and found a weird situation:
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));
}
}
}
The output is:
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.
How it is possible? Shouldn't be the file modified time after the new Date time? This is happening 1 in 4/5 tries.
What i'm missing in here?
Any way to guarantee that new Date() is older than file creation?