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

Java examples for java.util:Month

Description

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

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(sameHour(one, two));
    }/*from  w  w  w.  ja va2s. c om*/

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

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

    /**
     * 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