Skip to content

FTPClient.listFiles is not returning time in seconds

private static void getFTPFileProperties(FTPClient client,
            String ftpLocation, String pattern) throws IOException {
    FTPFile[] fileList=null;
    fileList = client.listFiles();
    for(int i=0;i<fileList.length;i++)
    {
        FTPFile file= fileList[0];
        Calendar cal = file.getTimestamp();
        DateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(dateFormater.format(cal.getTime()));
    }
}

I have written the above function to retrieve the file details. But somehow I am retrieving the details without seconds part of the file. I am retrieving the lastModifiedDate as 2013-08-08 00:00:00 where as its actual lastModifiedDate is 2013-08-08 12:53:27 PM

Answer

The FTPClient.listFiles uses the ancient LIST command. With the command, it’s quite common that the FTP server returns a listing similar to that of the Unix ls command. It displays timestamps with a day precision only, for old files (older than a year).

Nowadays, you should always use the FTPClient.mlistDir, which uses the modern MLSD command that always retrieves timestamps with second precision.

public FTPFile[] mlistDir() throws IOException

Of course, unless you connect to an ancient FTP server, that does not support the MLSD command.

Note that the mlistDir is supported since Apache Commons Net 3.0.