Checks whether the two calendar objects hold the same date value (year, month and day). - Java java.util

Java examples for java.util:Month

Description

Checks whether the two calendar objects hold the same date value (year, month and day).

Demo Code


//package com.java2s;
import java.util.Calendar;

public class Main {
    public static void main(String[] argv) throws Exception {
        Calendar first = Calendar.getInstance();
        Calendar second = Calendar.getInstance();
        System.out.println(equalDates(first, second));
    }//from  w ww. j  a v  a  2  s.com

    /**
     * Checks whether the two calendar objects hold the same date value (year, month and
     * day).
     *
     * @param first
     *            the first calendar object
     * @param second
     *            the second calendar object
     * @return true if the two calendar objects hold the same date value (year, month and
     *         day).
     */
    public static boolean equalDates(Calendar first, Calendar second) {
        return equalFields(first, second, Calendar.YEAR)
                && equalFields(first, second, Calendar.DAY_OF_YEAR);
    }

    /**
     * Checks whether the specified field in two calendar objects holds the same value.
     *
     * @param first
     *            the first calendar object
     * @param second
     *            the second calendar object
     * @param field
     *            the Calendar field to check
     * @return true if the specified field in two calendar objects holds the same value.
     */
    public static boolean equalFields(Calendar first, Calendar second,
            int field) {
        return (first == null || second == null) ? first == second : first
                .get(field) == second.get(field);
    }
}

Related Tutorials