4

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?

Christopher Perry
  • 38,891
  • 43
  • 145
  • 187
  • What is the time difference (measured in hours, minutes, seconds) between "now" and the timestamp your modified file has when you modify it "now"? Is that difference consistent when you do that multiple times? What functions are you using to set the threshold date, and to get the file modification date? – Jean Hominal Nov 10 '10 at 06:52
  • What is your reference time? Try comparing the time modified to `System.currentTimeMillis()`. – Computerish Nov 10 '10 at 23:20

4 Answers4

2

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.

bipen
  • 36,319
  • 9
  • 49
  • 62
Alexey
  • 21
  • 3
1

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?

Sinjo
  • 430
  • 3
  • 7
  • I'm using my personal device to test, and the date and time are set correctly. – Christopher Perry Nov 10 '10 at 01:22
  • Maybe some Android programmer forgot that computers count from zero? :) – drudge Nov 10 '10 at 01:25
  • @jnpcl: that's quite unlikely, I'd be much more suspicious about the time/timezone setting in the device. It's very likely you've set the wrong timezone setting, and sets the wrong time to offset the incorrect timezone setting; two wrongs can sometimes makes a right. – Lie Ryan Nov 10 '10 at 01:38
  • It's set to automatically set it. It's showing the correct date, and time, and time zone. I'm in CA, and it's set to PST. I tried another device (Nexus One), with the same settings, and it isn't having the same problem. I'm wondering if it's a device issue, or an issue that has to do with Android version now. – Christopher Perry Nov 10 '10 at 02:21
  • 1
    Are the file modifications set in GMT? – Falmarri Nov 10 '10 at 02:22
  • The files are only ever created, read, and deleted. I don't modify them. The function used to create the files is what I posted. – Christopher Perry Nov 10 '10 at 23:03
  • 1
    Why is this marked the solution if you first comment rules it out as the cause of the issue? – CrackerJack9 Aug 19 '11 at 17:03
0

Assuming that you are using java.io.File.lastModified(), to get the last-modified timestamp, you are getting the number of milliseconds since 00:00:00 GMT, January 1, 1970. The timestamp is implicitly GMT/UTC based.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I know that. So are the times I'm comparing. This works on one phone, but not another. They are running different versions of Android, so that's why I think this may be an Android issue. – Christopher Perry Nov 10 '10 at 23:04
0

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.

Gallal
  • 4,267
  • 6
  • 38
  • 61