I write current date time to a file (file may already exist or not) as content of the file. Later, I compare the date modified time of the file with the date time included in the file as content. However, date modified time is less than content date time. How can something that happened later (date modified time of the file) have a earlier time. Data content is json string, written as a text file. (note that: I don't set date modified time programmatically)
Here is my code
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
//this is how file content is formatted for writing.
writeFile(location, dateFormat.format(new Date()))
public static void writeFile(String location, String data) {
try {
PrintWriter output = new PrintWriter(
new File(location));
output.write(data);
output.close();
} catch (FileNotFoundException e) {
}
}
**To compare**
//read content from file
Date date = dateFormat.parse(dateString);
//get file modified time
Date diskTime = new Date(file.lastModified());
if(this.diskTime.getTime() < this.contentTime.getTime()
|| (this.diskTime.getTime() - this.contentTime.getTime()) > 2500)
{
return false;
}
The issue is diskTime sometimes (not always) a second before content time so above if case returns false.
Does this happen normally and how to overcome (create file always)?