-
Java last modified time
Hi there,
I have a question about the last modified time in java. I am returning the last modified time in two different ways, one is using the java.io.File and the second is using the new (i think its new) java.nio api.
The problem is that when I call the lastmodified() and the lastmodifiedtime(), I get two different answers, but the answer for the time is always different by 1 hour.
My modified 2007-22-06+15:57:36
Modified attr 2007-06-22+14:57:36
My modified 2007-26-07+15:16:26
Modified attr 2007-07-26+14:16:26
Am I doing something or do I just not understand the methods?
Any ideas / help greatly appreciated.
Code:
public static void fileIterator() {
Iterator it = FileUtils.iterateFiles(new File("/media/Vista"), null, true);
int i = 0;
File f = null;
FileTime ctime = null;
java.util.Date myDate = null;
String DATE_FORMAT_NOW = "yyyy-dd-MM+HH:mm:ss";
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT_NOW);
while (it.hasNext()) {
i++;
f = (File) it.next();
BasicFileAttributes attrs = null;
try {
attrs = Files.readAttributes(f.toPath(), BasicFileAttributes.class);
ctime = (FileTime) Files.getAttribute(f.toPath(), "unix:ctime");
} catch (IOException ex) {
Logger.getLogger(WindowsFileCall.class.getName()).log(Level.SEVERE, null, ex);
}
FileTime lastAccess = attrs.lastAccessTime();
myDate = new java.util.Date(f.lastModified());
sdf.format(myDate);
System.out.println(f.getName());
System.out.println("My modified " + sdf.format(myDate));
System.out.println("Modified attr " + attrs.lastModifiedTime().toString().replace("T", "+").replace("Z", ""));
System.out.println("Size " + org.apache.commons.io.FileUtils.sizeOf(f));
}
}
-
You do realise that ctime is the time of the last modification on the file's metadata?
By the way, that is a weird time syntax.
Edit:
atime is the time of the last access
mtime is the time of the last content modification
ctime is the time of the last metadata modification
creation time does not exist (by default)
Edit 2:
Looking at it [now] I can see why it is confusing.
-
Also have you check that method methods uses UTC (Zulu time)?
You wouldn't happen to be in England (or so) using daylight saving time (as UTC is always in standard time)?
-
Hi Hibernate,
I'm not in the uk but close, I am in in Ireland so same time zone.
I can see that f.lastModified() is returning GMT time but I'm not sure what attrs.lastModifiedTime() is returning, i couldn't see any thing in the Api about it. I am now looking attrs.lastModifiedTime().toString() to see what it is doing with the time.
-
Please do not every use GMT as a [proper] time zone, since it is not well defined (but…, just use UTC [or UT, UT0, UT1, UT2, TT, AT, …], it is always better). In Java GMT = UTC, in real time keep, it is not!
Referring to GMT does not tell if you are using standard time or daylight saving time.
Would assume Files.getAttribute(f.toPath(), "unix:ctime" /*as well as unix:mtime and unix:atime*/) returns file system native time, which should be UTC+0 [with positive leap seconds as the previous non-leap second].
It looks like f.lastModified() returns, not GMT [that is GMT as in UTC0], but [during the summer] BST (UTC0 + 1h advance due to daylight saving). And that is crappy, all time methods should return UTC and let you convert to civil time.
It also looks like attrs.lastModifiedTime() returns UTC.