file.lastModified() is never what was set with file.setLastModified()

前端 未结 5 1187
情深已故
情深已故 2020-12-19 01:16

I do have a problem with millis set and read on Android 2.3.4 on a Nexus One. This is the code:

File fileFolder = new File(Environment.getExternalStorageDire         


        
5条回答
  •  星月不相逢
    2020-12-19 01:47

    So maybe I'm missing something but I see some problems with your code above. Your specific problem may be due (as @JB mentioned) to Android issues but for posterity, I thought I'd provide an answer.

    First off, File.setLastModified() takes the time in milliseconds. Here are the javadocs. You seem to be trying to set it in seconds. So your code should be something like:

    fileFolder.setLastModified(1310198774000L);
    

    As mentioned in the javadocs, many filesystems only support seconds granularity for last-modification time. So if you need to see the same modification time in a file then you should do something like the following:

    private void changeModificationFile(File file, long time) {
        // round the value down to the nearest second
        file.setLastModified((time / 1000) * 1000);
    }
    

提交回复
热议问题