Search code examples
javafile-ioapache-commonsftp-clientlast-modified

How to compare FTPClient File's last modification with a local file?


I have two files one in the server and one is local, I want to get the last modification of both files and see which one is newer. I made a method, but I always get the same result. I tried the test each side, but the if statement make the same decition always.

Here is my code:

public void SyncCheck(FTPClient ftpClient, String remoteFilePath, String savePath) throws IOException, ParseException{
        String time = ftpClient.getModificationTime(remoteFilePath);
        Date remoteFileDate = timeSplitter(time);
        Date LocalFileDate = new Date(new File(savePath).lastModified());

        if(remoteFileDate.after(LocalFileDate)){
            System.out.println("Remote File is newer: " + remoteFileDate);
        }
        else{
            System.out.println("nothing");
            System.out.println("Remote " + remoteFileDate);
            System.out.println("Local " + LocalFileDate);
        }
    }

    public Date timeSplitter(String time) throws ParseException{
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String timePart = time.split(" ")[1];
        Date modificationTime = dateFormat.parse(timePart);
        return modificationTime;
}

The result is always this:

nothing
Remote Fri Apr 03 02:20:30 BST 2015
Local Fri Apr 03 03:12:58 BST 2015

No matter is the remote file is newer or older. The other this I notices is that the remote file is modified at 03:20:30, but it is one hour behind always. Is is about anything with time zones?

Or any idea to compare last modification time of one server file vs. a local one ?


Solution

  • There is no standard way to know ftp server timezone, but what you can do is to upload a file and then calculate the time difference between the file time reported by FTP and locally. This must be a method running as a first step of your program to initialize the timezone logic in every client application of yours.