为什么 Java(Android?)将我的文件的最后修改日期设置为明天?

发布于 2024-10-02 03:04:27 字数 403 浏览 1 评论 0原文

我正在我的 Android 应用程序中使用以下方法创建文件:

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

然后稍后我尝试进行一些清理,因此我将上次修改日期与某个阈值进行比较,但比较显示文件的上次修改日期是明天的某个时间。请记住,当我说明天时,这是相对于今天(文件修改日期)而言的......无论您正在阅读本文的哪一天。

所以基本上,如果我今天创建文件,并检查文件的上次修改日期,它会说它是明天最后修改的。

这是 Java 正在做的事情,还是 Android 正在做的事情,还是我正在做的事情?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

帅哥哥的热头脑 2024-10-09 03:04:27

我正在解决类似的问题(lastModified date 有时会在不触及文件的情况下更改),但没有找到解决方法。

我创建了一个创建文件的测试,存储 lastModified 函数返回的值,并在对手机执行一些操作后再次检查该值(我的设备是 Galaxy S)。

我发现 lastModified 在工作时使用内部缓存,并且可以通过启用/禁用 USB 模式清除该缓存(如果文件位于 SDCard 上) 。我还认为这个Cache会在一段时间后自动清除。如果缓存已被清除,则该函数将读取文件系统中存储的实际值。

如果您更改设备上的时区并清除缓存,则 lastModified 函数将返回另一个时间(差异取决于新时区,例如,如果您从 GMT 更改) +3 到 GMT+4 则时差为 1 小时)。

这个问题是100%可重现的。

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.

朮生 2024-10-09 03:04:27

您的测试设备/模拟器显示的当前日期和时间是什么?听起来他们只是设置不正确。

例如,设备上的时区是否设置为本地时间以外的时间?

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?

作死小能手 2024-10-09 03:04:27

假设您使用 java.io.File.lastModified() 来获取最后修改的时间戳,您将获取自 1970 年 1 月 1 日 00:00:00 GMT(格林尼治标准时间)以来的毫秒数。时间戳隐式基于 GMT/UTC。

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.

喜你已久 2024-10-09 03:04:27

您应该检查手机上可能使用 FileOutputStream 修改和创建的其他文件(可能您的照片)。如果他们也将修改日期设置为明天,那么我建议您向手机制造商提交错误报告。

我认为这是与手机相关的错误,因为您说过您的 Nexus One 上不会发生这种情况。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文