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!
To get the modified date and time for all files and sub folders in the current directory the command would be: To get modified date/time only for files in the current directory (i.e exclude directories from files) Using forfiles command we can get modified date and time for all the files in a directory.
Get Last Modified Date of a File in Linux with the command date The first method to learn the last modification date of a file in Linux explained in this tutorial focuses on the date command. When the command date is followed by the -r flag, it shows us the last modification date of a file.
You can run the below command to find the latest modified file in a directory. It would print the list of files in the order of file modified time. It would print the recently modified file at the bottom. /O:D will make the command print the files list using the file date/time attributes. /T:W will make the command use file modified time.
To get the modified date and time for all files and sub folders in the current directory the command would be: dir /T:W. To get modified date/time only for files in the current directory(i.e exclude directories from files) dir /T:W /A:-D.
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With