Returns a boolean indicating whether the given Date is in the same period as the Date in the calendar, as defined by the calendar field. - Java java.util

Java examples for java.util:Calendar Compare

Description

Returns a boolean indicating whether the given Date is in the same period as the Date in the calendar, as defined by the calendar field.

Demo Code

/*/* ww  w  .ja va 2 s  . co  m*/
 * $Id: CalendarUtils.java 3916 2011-01-12 10:21:58Z kleopatra $
 *
 * Copyright 2007 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
import java.util.Calendar;
import java.util.Date;

public class Main{
    public static void main(String[] argv) throws Exception{
        Calendar today = Calendar.getInstance();
        Date now = new Date();
        int field = 2;
        System.out.println(isSame(today,now,field));
    }
    public static final int DECADE = 5467;
    public static final int YEAR_IN_DECADE = DECADE + 1;
    /**
     * Returns a boolean indicating whether the given Date is in the same period as
     * the Date in the calendar, as defined by the calendar field. 
     * Calendar and date are unchanged by the check.
     *
     * @param today the Calendar representing a date, must not be null.
     * @param now the date to compare to, must not be null
     * @return true if the calendar and date represent the same day in the
     *   given calendar.
     */
    public static boolean isSame(Calendar today, Date now, int field) {
        Calendar temp = (Calendar) today.clone();
        startOf(temp, field);
        Date start = temp.getTime();
        temp.setTime(now);
        startOf(temp, field);
        return start.equals(temp.getTime());
    }
    /**
     * Adjusts the given calendar to the start of the period as indicated by the
     * given field. This delegates to startOfDay, -Week, -Month, -Year as appropriate.
     * 
     * @param calendar
     * @param field the period to adjust, allowed are Calendar.DAY_OF_MONTH, -.MONTH, 
     * -.WEEK and YEAR and CalendarUtils.DECADE.
     */
    public static void startOf(Calendar calendar, int field) {
        switch (field) {
        case Calendar.DAY_OF_MONTH:
            startOfDay(calendar);
            break;
        case Calendar.MONTH:
            startOfMonth(calendar);
            break;
        case Calendar.WEEK_OF_YEAR:
            startOfWeek(calendar);
            break;
        case Calendar.YEAR:
            startOfYear(calendar);
            break;
        case DECADE:
            startOfDecade(calendar);
            break;
        default:
            throw new IllegalArgumentException("unsupported field: "
                    + field);

        }
    }
    /**
     * Adjust the given calendar to the first millisecond of the given date.
     * that is all time fields cleared. The Date of the adjusted Calendar is
     * returned. 
     * 
     * @param calendar calendar to adjust.
     * @param date the Date to use.
     * @return the start of the day of the given date
     */
    public static Date startOfDay(Calendar calendar, Date date) {
        calendar.setTime(date);
        startOfDay(calendar);
        return calendar.getTime();
    }
    /**
     * Adjust the given calendar to the first millisecond of the current day.
     * that is all time fields cleared.
     * 
     * @param calendar calendar to adjust.
     */
    public static void startOfDay(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.getTimeInMillis();
    }
    /**
     * Adjusts the calendar to the start of the current month.
     * That is, first day of the month with all time fields cleared.
     * 
     * @param calendar calendar to adjust.
     */
    public static void startOfMonth(Calendar calendar) {
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        startOfDay(calendar);
    }
    /**
     * Adjusts the calendar to the start of the current week.
     * That is, first day of the week with all time fields cleared.
     * @param calendar the calendar to adjust.
     * @return the Date the calendar is set to
     */
    public static void startOfWeek(Calendar calendar) {
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
        startOfDay(calendar);
    }
    /**
     * Adjusts the calendar to the start of the current week.
     * That is, last day of the week with all time fields at max.
     * The Date of the adjusted Calendar is
     * returned. 
     * 
     * @param calendar calendar to adjust.
     * @param date the Date to use.
     * @return the start of the week of the given date
     */
    public static Date startOfWeek(Calendar calendar, Date date) {
        calendar.setTime(date);
        startOfWeek(calendar);
        return calendar.getTime();
    }
    /**
     * Adjusts the given Calendar to the start of the year.
     * 
     * @param calendar the calendar to adjust.
     */
    public static void startOfYear(Calendar calendar) {
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        startOfMonth(calendar);
    }
    /**
     * Adjusts the given Calendar to the start of the year as defined by 
     * the given date. Returns the calendar's Date.
     * 
     * @param calendar calendar to adjust.
     * @param date the Date to use.
     * @return the start of the year of the given date
     */
    public static Date startOfYear(Calendar calendar, Date date) {
        calendar.setTime(date);
        startOfYear(calendar);
        return calendar.getTime();
    }
    /**
     * Adjusts the given Calendar to the start of the decade.
     * 
     * @param calendar the calendar to adjust.
     */
    public static void startOfDecade(Calendar calendar) {
        calendar.set(Calendar.YEAR, decade(calendar.get(Calendar.YEAR)));
        startOfYear(calendar);
    }
    /**
     * Adjusts the given Calendar to the start of the decade as defined by 
     * the given date. Returns the calendar's Date.
     * 
     * @param calendar calendar to adjust.
     * @param date the Date to use.
     * @return the start of the decade of the given date
     */
    public static Date startOfDecade(Calendar calendar, Date date) {
        calendar.setTime(date);
        startOfDecade(calendar);
        return calendar.getTime();
    }
    /**
     * Sets the calendar field of the given calendar by amount. <p>
     * 
     * NOTE: the custom field implementations are very naive (JSR-310 will do better)
     * - for decade: value must be positive, value must be a multiple of 10 and is interpreted as the 
     *    first-year-of-the-decade
     * - for year-in-decade:  value is added/substracted to/from the start-of-decade of the
     *   date of the given calendar
     * 
     * @param calendar
     * @param field the field to increment, allowed are all fields known to
     *   Calendar plus DECADE.
     * @param value the decade to set, must be a 
     * 
     * @throws IllegalArgumentException if the field is unsupported or the value is
     *    not dividable by 10 or negative.
     */
    public static void set(Calendar calendar, int field, int value) {
        if (isNativeField(field)) {
            calendar.set(field, value);
        } else {
            switch (field) {
            case DECADE:
                if (value <= 0) {
                    throw new IllegalArgumentException(
                            "value must be a positive but was: " + value);
                }
                if (value % 10 != 0) {
                    throw new IllegalArgumentException(
                            "value must be a multiple of 10 but was: "
                                    + value);
                }
                int yearInDecade = get(calendar, YEAR_IN_DECADE);
                calendar.set(Calendar.YEAR, value + yearInDecade);
                break;
            case YEAR_IN_DECADE:
                int decade = get(calendar, DECADE);
                calendar.set(Calendar.YEAR, value + decade);
                break;
            default:
                throw new IllegalArgumentException("unsupported field: "
                        + field);
            }

        }
    }
    /**
     * @param year
     * @return
     */
    private static int decade(int year) {
        return (year / 10) * 10;
    }
    /**
     * Gets the calendar field of the given calendar by amount. 
     * 
     * @param calendar
     * @param field the field to get, allowed are all fields known to
     *   Calendar plus DECADE.
     * 
     * @throws IllegalArgumentException
     */
    public static int get(Calendar calendar, int field) {
        if (isNativeField(field)) {
            return calendar.get(field);
        }
        switch (field) {
        case DECADE:
            return decade(calendar.get(Calendar.YEAR));
        case YEAR_IN_DECADE:
            return calendar.get(Calendar.YEAR) % 10;
        default:
            throw new IllegalArgumentException("unsupported field: "
                    + field);
        }
    }
}

Related Tutorials