Java tutorial
//package com.java2s; /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.File; import java.util.Date; public class Main { /** * Tests if the 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, must not be {@code null} * @param reference the <code>File</code> of which the modification date * is used, must not be {@code null} * @return true if the <code>File</code> exists and has been modified more * recently than the reference <code>File</code> * @throws IllegalArgumentException if the file is {@code null} * @throws IllegalArgumentException if the reference file is {@code null} or doesn't exist */ public static boolean isFileNewer(File file, File reference) { if (reference == null) { throw new IllegalArgumentException("No specified reference file"); } if (!reference.exists()) { throw new IllegalArgumentException("The reference file '" + reference + "' doesn't exist"); } return isFileNewer(file, reference.lastModified()); } /** * Tests if the specified <code>File</code> is newer than the specified * <code>Date</code>. * * @param file the <code>File</code> of which the modification date * must be compared, must not be {@code null} * @param date the date reference, must not be {@code null} * @return true if the <code>File</code> exists and has been modified * after the given <code>Date</code>. * @throws IllegalArgumentException if the file is {@code null} * @throws IllegalArgumentException if the date is {@code null} */ public static boolean isFileNewer(File file, Date date) { if (date == null) { throw new IllegalArgumentException("No specified date"); } return isFileNewer(file, date.getTime()); } /** * 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, must not be {@code null} * @param timeMillis the time reference measured in milliseconds since the * epoch (00:00:00 GMT, January 1, 1970) * @return true if the <code>File</code> exists and has been modified after * the given time reference. * @throws IllegalArgumentException if the file is {@code null} */ public static boolean isFileNewer(File file, long timeMillis) { if (file == null) { throw new IllegalArgumentException("No specified file"); } if (!file.exists()) { return false; } return file.lastModified() > timeMillis; } }