Java Day Compare sameDay(Date d1, Date d2)

Here you can find the source of sameDay(Date d1, Date d2)

Description

test two dates have the same day

License

Apache License

Parameter

Parameter Description
d1 non-null date
d2 non-null date

Return

as above

Declaration

@SuppressWarnings(value = "deprecated")
public static boolean sameDay(Date d1, Date d2) 

Method Source Code

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

import java.util.*;

public class Main {
    /**//from w  w  w.  java 2s  .co m
     * test two dates have the same day
     * @param d1  non-null date
     * @param d2   non-null date
     * @return as above
     */
    @SuppressWarnings(value = "deprecated")
    public static boolean sameDay(Date d1, Date d2) {
        if (d1.getYear() != d2.getYear())
            return false;
        if (d1.getMonth() != d2.getMonth())
            return false;
        if (d1.getDay() != d2.getDay())
            return false;
        return true;
    }

    public static int getYear(Date date) {
        GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance();
        long time = date.getTime();
        calendar.setTimeInMillis(time);
        return calendar.get(calendar.YEAR);
    }

    /**
     * @param date Date
     * @return int month number 0-based
     */
    public static int getMonth(Date date) {
        GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance();
        long time = date.getTime();
        calendar.setTimeInMillis(time);
        return calendar.get(calendar.MONTH);
    }
}

Related

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