相关文章推荐
不敢表白的钱包  ·  node: ...·  1 月前    · 
任性的包子  ·  SqlAlchemy 2.0 ...·  4 月前    · 
会开车的夕阳  ·  about Functions ...·  4 月前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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!

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";
                Thanks for your response.  Well what I am wanting is to display when each of the files was modified in a list.  It is not a big deal to me if they are sorted correctly.
– user2250122
                Sep 14, 2013 at 17:26
                @user2250122 : i think from List of files you can get the lastModified date of all files see updated answer which clarifies and list all modified date for all files in directory
– Trikaldarshiii
                Sep 14, 2013 at 17:36
    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()));

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.

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;
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.