-2
import java.io.*;
import java.util.*;
import java.text.*;

public class test
{

 public static void main(String[] args)
{
  String path = "C:/stuff/";

  String files;
  File folder = new File(path);
  File[] listOfFiles = folder.listFiles();

 for (int i = 0; i < listOfFiles.length; i++)
  {
      System.out.println(listOfFiles[i].getName());
  }
}
}

this is my code. i want to display timestamps [last modified] details along with the file name in a sorted manner. please help..

aarit
  • 1
  • 2
  • 3
    http://stackoverflow.com/questions/14372822/sorting-files-by-last-modified-in-java-efficient-way – Tarmo Feb 21 '14 at 08:34

3 Answers3

0

File Class provide function to get last modify date for that file.

You may use below code to get last modify date :

File file = new File("Your_File_Name");

System.out.println("Before Format : " + file.lastModified());

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

System.out.println("After Format : " + sdf.format(file.lastModified()));  

Here is JAVA Doc Reference.

For sorting file list based on File Name you may refer This Question.

Community
  • 1
  • 1
Yagnesh Agola
  • 4,556
  • 6
  • 37
  • 50
0
for (int i = 0; i < listOfFiles.length; i++)
{
  System.out.println(listOfFiles[i].getName()+"\t"+new Date(listOfFiles[i].lastModified()));
}
Rahul
  • 3,479
  • 3
  • 16
  • 28
  • it helped.also found another way to do that by using LONG variable to store lastModified() and then print it using +new Date(). anyways, both are same. THANKS :) – aarit Feb 21 '14 at 09:55
0

About display timestamp you can do as follows:

System.out.println(String.format("%s-%s", listOfFiles[i].getName(), listOfFiles[i].lastModified()));

listOfFiles[i].lastModified() return the timestamp. If you need change it to Date, you can reference the link as follows: Pick day, month and year out file.lastModified();.

About display the filename in sorted manner, you can reference the link as follows: Best way to list files in Java, sorted by Date Modified?

Community
  • 1
  • 1
Cphilo
  • 46
  • 11