获取上次修改文件的日期

时间:2012-12-05 14:46:29

标签: android android-file

我正在编写一个方法来检查文件夹中的最后一个文件是否只有一天。

 private void checkForPhotoDelete() {
        File directory = new File("/sdcard/LC/images");
        File[] files = directory.listFiles();
        if(files.length>0){
            File lastFile = files[files.length-1];
            Date lastMod = new Date(lastFile.lastModified());

            Calendar calendar = Calendar.getInstance();
            int today = calendar.get(Calendar.DAY_OF_WEEK); 
            Log.v("last modified","Last Modified Date : "+lastMod.getDay() + " vs "+today);
        }
        // TODO Auto-generated method stub

    }

问题是我的文件最后一次修改是在星期二,今天是结婚。所以我希望我的日志说:

  

“上次修改日期:3对4”

但是它说的是2对4

1 个答案:

答案 0 :(得分:0)

尝试将calendar实例设置为当前日期:

private void checkForPhotoDelete() {
        File directory = new File("/sdcard/LC/images");
        File[] files = directory.listFiles();
        if(files.length>0){
            File lastFile = files[files.length-1];
            long lastMod = lastFile.lastModified();

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            int today = calendar.get(Calendar.DAY_OF_WEEK); 
            calendar.setTimeInMillis(lastMod);
            int lastModifyDay =  calendar.get(Calendar.DAY_OF_WEEK); 
            Log.v("last modified","Last Modified Date : "+ lastModifyDay + " vs "+today);
        }
        // TODO Auto-generated method stub

    }