Java Day Compare sameDay(Date date1, Date date2)

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

Description

same Day

License

Apache License

Parameter

Parameter Description
date1 a date
date2 another date

Return

true of date1 and date2 are in the same day, false otherwise.

Declaration

public static boolean sameDay(Date date1, Date date2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /**// ww w .  j a va2s  .c  o m
     * @param date1 a date
     * @param date2 another date
     * @return <code>true</code> of <code>date1</code> and <code>date2</code> are in the same day,
     *         <code>false</code> otherwise.
     */
    public static boolean sameDay(Date date1, Date date2) {
        if (date1 == null || date2 == null)
            throw new IllegalArgumentException("'date1' or 'date2' was null");

        Calendar c1 = Calendar.getInstance();
        c1.setTime(date1);

        Calendar c2 = Calendar.getInstance();
        c2.setTime(date2);

        return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)
                && c1.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR);
    }
}

Related

  1. isSameDay(Date day1, Date day2)
  2. isSameDay(Date preDate, Date postDate)
  3. isSameDay(final Date date1, final Date date2)
  4. isSameDay(final Date date1, final Date date2)
  5. sameDay(Date d1, Date d2)