Prashant
Prashant

Reputation: 1469

How can i get updated date and time of file which is present in android device?

How can i have updated date and time for a file which is present on sd card of android device? How can i get the same programmatically?

Thanks in advance!!!

Upvotes: 0

Views: 1508

Answers (2)

GrIsHu
GrIsHu

Reputation: 23638

Just check the existence of directory..

public long lastModified ()

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.

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();
    }

I hope this will help you.

Upvotes: 1

Hayk Nahapetyan
Hayk Nahapetyan

Reputation: 4550

I think It'll help you

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

you can read more about lastModified from here

Regards Hayk Nahapetyan

Upvotes: 2

Related Questions