Java Is File Newer isNewer(File file, File reference)

Here you can find the source of isNewer(File file, File reference)

Description

PerformTest if specified File is newer than the reference File.

License

Open Source License

Parameter

Parameter Description
file the <code>File</code> of which the modification date must be compared
reference the <code>File</code> of which the modification date is used

Return

true if the File exists and has been modified more recently than the reference File.

Declaration

public static boolean isNewer(File file, File reference) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

public class Main {
    /**/*ww  w.  j a  v a2  s. c om*/
     * PerformTest if specified <code>File</code> is newer than the reference <code>File</code>.
     *
     * @param file      the <code>File</code> of which the modification date must be compared
     * @param reference   the <code>File</code> of which the modification date is used
     * @return <code>true</code> if the <code>File</code> exists and has been modified more
     *          recently than the reference <code>File</code>.
     */
    public static boolean isNewer(File file, File reference) {
        if (reference.exists() == false) {
            throw new IllegalArgumentException("The reference file '"
                    + file + "' doesn't exist");
        }
        return isNewer(file, reference.lastModified());
    }

    /**
     * Tests if the specified <code>File</code> is newer than the specified time reference.
     *
     * @param file         the <code>File</code> of which the modification date must be compared.
     * @param timeMillis   the time reference measured in milliseconds since the
     *                   epoch (00:00:00 GMT, January 1, 1970)
     * @return <code>true</code> if the <code>File</code> exists and has been modified after
     *         the given time reference.
     */
    public static boolean isNewer(File file, long timeMillis) {
        if (!file.exists()) {
            return false;
        }
        return file.lastModified() > timeMillis;
    }

    public static boolean isNewer(String file, long timeMillis) {
        return isNewer(new File(file), timeMillis);
    }

    public static boolean isNewer(String file, String reference) {
        return isNewer(new File(file), new File(reference));
    }
}

Related

  1. isNewer(File file1, File file2)
  2. isNewer(String file, String reference)
  3. isNewerOrEqual(String newFile, String oldFile)