Java Day Compare isSameDay(Date date1, Date date2)

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

Description

Checks if two dates are the same day

License

Open Source License

Parameter

Parameter Description
date1 the first date
date2 the second date

Return

true if dates are same day

Declaration

public static boolean isSameDay(Date date1, Date date2) 

Method Source Code

//package com.java2s;
/**//from w w  w.j  a  v  a 2 s . c o m
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.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://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */

import java.util.*;

public class Main {
    /**
     * Checks if two dates are the same day
     * @param date1 the first date
     * @param date2 the second date
     * @return true if dates are same day
     */
    public static boolean isSameDay(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return false;
        }

        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
                && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH);
    }
}

Related

  1. isSameDay(Date date, Date date2)
  2. isSameDay(Date date1, Date date2)
  3. isSameDay(Date date1, Date date2)
  4. isSameDay(Date date1, Date date2)
  5. isSameDay(Date date1, Date date2)
  6. isSameDay(Date date1, Date date2)
  7. isSameDay(Date day1, Date day2)
  8. isSameDay(Date day1, Date day2)
  9. isSameDay(Date preDate, Date postDate)