Returns true if the two given calendars are dated on the same year and month. - Java java.util

Java examples for java.util:Month

Description

Returns true if the two given calendars are dated on the same year and month.

Demo Code


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

public class Main {
    public static void main(String[] argv) throws Exception {
        Calendar one = Calendar.getInstance();
        Calendar two = Calendar.getInstance();
        System.out.println(sameMonth(one, two));
    }/*from  ww  w . ja  v  a2 s.c o  m*/

    /**
     * Returns <tt>true</tt> if the two given calendars are dated on the same
     * year and month.
     * 
     * @param one
     *            The one calendar.
     * @param two
     *            The other calendar.
     * @return True if the two given calendars are dated on the same year and
     *         month.
     */
    public static boolean sameMonth(Calendar one, Calendar two) {
        return one.get(Calendar.MONTH) == two.get(Calendar.MONTH)
                && sameYear(one, two);
    }

    /**
     * Returns <tt>true</tt> if the two given calendars are dated on the same
     * year.
     * 
     * @param one
     *            The one calendar.
     * @param two
     *            The other calendar.
     * @return True if the two given calendars are dated on the same year.
     */
    public static boolean sameYear(Calendar one, Calendar two) {
        return one.get(Calendar.YEAR) == two.get(Calendar.YEAR);
    }
}

Related Tutorials