views:

231

answers:

3

I set the modification date of a file created in Java to a specific number. When I read out that value on Windows, I get the same value out. However, on Linux (ubuntu) I get a different value. The value for File.lastModified() is off by 9 hours, yet when I look a the file properties I see that it's off by 1 hour only. I am expecting the same value across the board.

Am I wrong to depend on this being compatible and consistent? The javadoc is pretty unambiguous on the meaning of the method and does not mention potential incompatibilities.

A: 

did you check the return value of setLastModified?

Returns:

true if and only if the operation succeeded; false otherwise
dfa
The date is significantly different, so something certainly did change.
Wouter Lievens
+1  A: 

It's almost certainly a time zone issue. The Java method uses/expects GMT, the OS will display local time, which accounts for the difference there. Now the real question is: how is the time stored in the file system?

What file system are you using? Probably FAT32 - which stores timestamps in local time, thus making it hard to keep them consistent across OSs. I'm not sure where exactly things go wrong, but it may be an OS configuration issue or a JVM bug - which JVM are you using on Linux?

Michael Borgwardt
The file is on a USB stick, so I don't know what file system applies there. But the time zone thing makes sense.
Wouter Lievens
USB sticks almost always use FAT32.
Michael Borgwardt
A: 

My guess is that it's a timezone problem. Note that the javadoc says "milliseconds since the epoch (00:00:00 GMT, January 1, 1970)" (emphasis added). Is it possible that the value you passed into setModified was milliseconds since the epoch, local time? If so, then you would be one hour out since local time in Belgium is GMT + 1. That would explain the time in the properties dialog.

I'm at a loss to explain the 9 hour difference from lastModified(), unless java or the os is caching an old value somehow.

Sean Reilly
The timezone thing makes sense. I'll look into that.
Wouter Lievens