Android的:如何获得和放大器;集目录修改日期 [英] Android: How to get & set directory modification date

查看:181
本文介绍了Android的:如何获得和放大器;集目录修改日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方式来获得一个目录的修改日期。我试过:

I'm looking for a way to get a directories modification date. I've tried:

File dir = new File(myDir);
long mod = dir.lastModified();

但它返回0。

我也在寻找一种方法来设置一个目录的最后修改日期,但没有发现任何东西。

I'm also looking for a way to set the last modification date of a directory but haven't found anything.

是否有文件的方式来做到这些?

Is there a documented way to do these?

推荐答案

编辑:
您code看起来正确,只是检查目录是否存在。

Your code looks right, Just check the existence of directory..

public long lastModified ()

返回此文件的最后修改,自1970年1月1日午夜以毫秒计的时间。 返回0,如果该文件不存在。

所以只是检查您的文件是否是存在或不..

So just check whether your file is exist or not..

code:

有关获得文件最后修改日期,

For get Last modified date from file,

File file = new File("Your file path");
Date lastModDate = new Date(file.lastModified());
Log.i("File last modified : "+ lastModDate.toString());

要设置上次修改日期的文件。

To set Last Modified date to a file..

try{

    File file = new File("/mnt/sdcard/temp.txt");

    //print the original last modified date
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    Log.i("Original Last Modified Date : " , ""+sdf.format(file.lastModified()));

    //set this date 
    String newLastModified = "01/06/2012";

    //need convert the above date to milliseconds in long value 
    Date newDate = sdf.parse(newLastModified);
    file.setLastModified(newDate.getTime());

    //print the latest last modified date
    Log.i("Lastest Last Modified Date : ", ""+sdf.format(file.lastModified()));

    }catch(ParseException e){
        e.printStackTrace();
    }

这篇关于Android的:如何获得和放大器;集目录修改日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆