Java Date Equal datesEqualToSecond(Date date1, Date date2)

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

Description

compares two datess down to the second

License

Apache License

Parameter

Parameter Description
date1 first date
date2 second date

Return

true if the dates equal to the second, false otherwise

Declaration

public static boolean datesEqualToSecond(Date date1, Date date2) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w . j a  v  a2 s .com*/
 * Copyright 2006-2008 Appcelerator, Inc.
 * 
 * Licensed 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.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import java.util.TimeZone;

public class Main {
    /**
     * Greenwich mean time timezone (GMT).
     */
    public static final TimeZone GMT_TZ = TimeZone.getTimeZone("GMT");

    /**
     * compares two datess down to the second
     *
     * @param date1 first date
     * @param date2 second date
     * @return <code>true</code> if the dates equal to the second, <code>false</code> otherwise
     */
    public static boolean datesEqualToSecond(Date date1, Date date2) {
        Calendar calendar1 = new GregorianCalendar(GMT_TZ);
        calendar1.setTime(date1);
        Calendar calendar2 = new GregorianCalendar(GMT_TZ);
        calendar2.setTime(date2);

        if (calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) {
            if (calendar1.get(Calendar.DAY_OF_YEAR) == calendar2.get(Calendar.DAY_OF_YEAR)) {
                if (calendar1.get(Calendar.HOUR) == calendar2.get(Calendar.HOUR)) {
                    if (calendar1.get(Calendar.MINUTE) == calendar2.get(Calendar.MINUTE)) {
                        if (calendar1.get(Calendar.SECOND) == calendar2.get(Calendar.SECOND)) {
                            return true;
                        }
                    }
                }
            }
        }

        return false;
    }
}

Related

  1. areEqual(Date date1, Date date2)
  2. dateEquals(Date dtFirst, Date dtSecond)
  3. equal(Date date, Date other)
  4. equalDateByDay(Date date1, Date date2)
  5. equalDates(Date first, Date second)
  6. equalsIgnoreTime(final Date d1, final Date d2)