【问题标题】:android, lastModified() is 1.1.1970安卓,lastModified() 是 1.1.1970
【发布时间】:2014-02-21 13:37:26
【问题描述】:

我想获取 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找不到文件?为什么?

【问题讨论】:

  • 看起来确实找不到文件。 Returns the time when this file was last modified, measured in milliseconds since January 1st, 1970, midnight. Returns 0 if the file does not exist.

标签: android file last-modified


【解决方案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);
}

【讨论】:

    猜你喜欢
    • 2011-04-22
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    相关资源
    最近更新 更多