Question

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.

Was it helpful?

Solution

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?

OTHER TIPS

did you check the return value of setLastModified?

Returns:

true if and only if the operation succeeded; false otherwise

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top