简体   繁体   中英

Why is Java (Android?) setting my file's last modified date to tomorrow?

I'm creating files in my Android application using:

FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
fos.write(data);
fos.flush();
fos.close();

Then later I'm trying to do some cleanup, so I'm comparing the last modified date to some threshold value, but the comparison is showing that the file's last modified date is tomorrow sometime. Keep in mind, when I say tomorrow, that is relative to today (the file modification date)... whichever day this may be that you're reading this.

So basically, if I create the file today, and check the file's last modification date, it says it was last modified tomorrow.

Is this something Java is doing, or Android, or something I'm doing?

What does your test device/emulator show the current date and time as? It sounds like they're just set incorrectly.

Is the time zone on the device set to something other than your local time for instance?

I work on the similar problem ( lastModified date is changed sometimes without touching to the file) but not find a workaround.

I create a test for create file, store value returned by lastModified function and check this value again after some actions to the phone (my device is Galaxy S ).

I found that lastModified uses internal cache while work and this cache can be cleared by enabling/disabling USB mode (if a file is on SDCard ). I think also that this Cache is cleared automatically after some interval. If cache has been cleared then the function read real value stored in the filesystem.

If you change the timezone on the device and clear the cache then lastModified function will return another time (the difference depends on the new timezone, for example, if you change from GMT+3 to GMT+4 then the difference is 1 hour).

This problem is 100% reproducible.

假设您使用java.io.File.lastModified()来获取上次修改的时间戳,您将获取自1970年1月1日00:00:00 GMT以来的毫秒数。该时间戳隐式为GMT / UTC基于。

You should check other files on your phone that may be modified and created using FileOutputStream ( possibly your photos). If they too have a modified date set to tomorrow, then I suggest you file a bug report to the manufacturers of your phone.

I assume it's a bug related to the phone because you said that this does not happen on your Nexus One.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM