android,lastModified()是1.1.1970

时间:2014-02-21 13:37:26

标签: android file last-modified

我想让lastModified()来比较日期。 我这样做:

FileWriter file = new FileWriter(getFilesDir()+File.separator + "openliga.txt");
file.write(changeObj.toString());
file.flush();
file.close();

我通过DDMS和文件浏览器检查,文件存在!

然后

// Check the Time Stamp of the internal File
File intfile = new File(getFilesDir()+File.separator + "openliga.txt");

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String dateInt = sdf.format(intfile.lastModified());

Log.e("LastMod", String.valueOf(dateInt));

告诉我:

Thu Jan 01 01:00:00 GMT+01:00 1970

有可能,android没有找到该文件?为什么呢?

1 个答案:

答案 0 :(得分:1)

您无法直接将intfile.lastModified()格式化为日期,因为intfile.lastModified()是以毫秒为单位的时间。因此需要添加日期,然后根据需要格式化日期。

修改 您还需要检查文件是否存在。

试试这个

if (intfile.exists()) {

   long lastmodified = intfile.lastModified();
   Date date = new Date();
   date.setTime(lastmodified);
   SimpleDateFormat postFormater = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

   String dateInt = postFormater.format(date);
}