Java Date Compare compareFileDates(final Date date1, final Date date2)

Here you can find the source of compareFileDates(final Date date1, final Date date2)

Description

For some reason modification milliseconds for some files are unstable, use this function to compare file dates ignoring milliseconds.

License

Open Source License

Parameter

Parameter Description
date1 first file modification date
date2 second file modification date

Return

true if files modification dates are equal ignoring milliseconds

Declaration

public static boolean compareFileDates(final Date date1, final Date date2) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 * Java Plug-in Framework (JPF)//  w ww .j  a  v a  2s .co  m
 * Copyright (C) 2004-2006 Dmitry Olshansky
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *****************************************************************************/

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Main {
    /**
     * For some reason modification milliseconds for some files are unstable,
     * use this function to compare file dates ignoring milliseconds.
     * @param date1 first file modification date
     * @param date2 second file modification date
     * @return <code>true</code> if files modification dates are equal ignoring
     *         milliseconds
     */
    public static boolean compareFileDates(final Date date1, final Date date2) {
        if ((date1 == null) || (date2 == null)) {
            return false;
        }
        Calendar cldr = Calendar.getInstance(Locale.ENGLISH);
        cldr.setTime(date1);
        cldr.set(Calendar.MILLISECOND, 0);
        long dt1 = cldr.getTimeInMillis();
        cldr.setTime(date2);
        cldr.set(Calendar.MILLISECOND, 0);
        long dt2 = cldr.getTimeInMillis();
        return dt1 == dt2;
    }
}

Related

  1. compareDatesOnly(long date1, long date2)
  2. compareDateTimeNull(Date d1, Date d2)
  3. compareDay(Date date, Date anotherDate)
  4. compareDay(Date date1, int compday)
  5. compareDays(final Date date1, final Date date2)
  6. compareMonth(Date first, Date second)
  7. compareSecond(java.util.Date date1, java.util.Date date2)
  8. compareStartDateEndDate(String startDate, String endDate)
  9. compareTime(Date date1, Date date2)