12

I am having some trouble displaying the files currently modified date.

public class MyAdapter extends ArrayAdapter<String> {

String dir = "/FileDirectory/";

File file = new File(Environment.getExternalStorageDirectory() + dir);

private final Activity context;

Date lastModified = new Date(file.lastModified()); 
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");  
String formattedDateString = formatter.format(lastModified); 

private String dateFormat = "dd/MM/yyyy HH:mm:ss";

static class ViewHolder {

    public TextView text;
}

public MyAdapter(Activity context, String[] date) {
    super(context, R.layout.row, date);
    this.context = context;
    formatter = new SimpleDateFormat(dateFormat);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View rowView = convertView;
    if (rowView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.row, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) rowView.findViewById(R.id.FilePath);
        rowView.setTag(viewHolder);
    }

    ViewHolder holder = (ViewHolder) rowView.getTag();

    String s = formattedDateString;

    holder.text.setText(s);

        return rowView;
    }
}

As of now it just displays the last modified date of the directory on all files. Any help would be greatly appreciated!

Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
user2250122
  • 153
  • 1
  • 1
  • 9

4 Answers4

11

In your question, you are pointing a Directory, not a File.

File file = new File(Environment.getExternalStorageDirectory() + dir);


private final Activity context;

Date lastModified = new Date(file.lastModified()); 
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");  
String formattedDateString = formatter.format(lastModified);

The idea is to get a Directory and iterate searching for the Last modified date of all files. The following question may help: How to get only 10 last modified files from directory using Java?

EDIT 1:

My Tricky Solution:

File images = new File("YourDirectoryPath");
long[] fileModifieDate = new long[images.listFiles().length];
int i=0;

        File[] imagelist = images.listFiles(new FilenameFilter()
        {

            public boolean accept(File dir, String name)
            {
                File file = new File(dir, name);
                fileModifieDate[i++] = file.lastModified();
                return true;
            }

        });
// Here, max is the last modified date for this directory 
// Here, Long array **fileModifieDate** will give modified time of all files, which you can also access from Files array
// if you want the last modified file in the directory you can do this:


        File[] maxModifiedDate = images.listFiles(new FilenameFilter()
        {

            public boolean accept(File dir, String name)
            {
                File file = new File(dir, name);

                return file.lastModified() == max;
            }

        });

// Now **maxModifiedDate** File array will have only one File, which will have max modified date.

EDIT 2:

For your case, this would be helpful:

public class MyAdapter extends ArrayAdapter<String> {

String dir = "/FileDirectory/";

File myFolder= new File(Environment.getExternalStorageDirectory() + dir);
if(myFolder.exists()){


    File[] filelist = myFolder.listFiles(new FilenameFilter()
    {

        public boolean accept(File dir, String name)
        {  
            return true;
        }

    }); 
}   

// Now you have a filelist array of Files. If you want lastModified data, you can fetch from each individual file as you were doing previously:

  private final Activity context;

    Date lastModified = new Date(file.lastModified()); 
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");  
    String formattedDateString = formatter.format(lastModified); 

    private String dateFormat = "dd/MM/yyyy HH:mm:ss";
Community
  • 1
  • 1
Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
3

Complete code to get the last modified date

    File file = new File("c:\\test\\myfile.txt");
    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
    System.out.println("Modified Date :- " + sdf.format(file.lastModified()));
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Vicky
  • 9,515
  • 16
  • 71
  • 88
0

File file = new File(Environment.getExternalStorageDirectory() + dir);

Points to directory, not a file.

If you need modified date of each file separately in a directory, just use walkfiletree.

Check this.

Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
Boy
  • 1,182
  • 2
  • 11
  • 28
0

I tried the answer of Anonymous Mohit, but it only checks the file size of items inside the folder. But what if there are many subfolders? Here's how I did it.

public static long lastModifiedDate;
public static long getLastModified(String path) {
    File directory = new File(path);
    File[] files = directory.listFiles();
    if(GlobalValues.DEBUG) Log.i(TAG, "Get last modified date: " + path);
    if (files.length == 0) return directory.lastModified();
    else {
        for(File filesInFolder: files) {
            if(filesInFolder.isDirectory()) {
                getLastModified(filesInFolder.getAbsolutePath());
            } else {
                if(lastModifiedDate == 0) {
                    lastModifiedDate = filesInFolder.lastModified();
                } else{
                    if(filesInFolder.lastModified() >= lastModifiedDate){
                        lastModifiedDate = filesInFolder.lastModified();
                    }
                }
            }
        }
        return lastModifiedDate;
    }
}
Omatt
  • 8,564
  • 2
  • 42
  • 144