Returns a boolean indicating if the calendar is set to the start of a period as defined by the given field. - Java java.util

Java examples for java.util:Calendar Compare

Description

Returns a boolean indicating if the calendar is set to the start of a period as defined by the given field.

Demo Code

/*/*from  ww  w .  j a  v a 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
 */
//package com.java2s;
import java.util.Calendar;

public class Main {
    public static void main(String[] argv) throws Exception {
        Calendar calendar = Calendar.getInstance();
        int field = 2;
        System.out.println(isStartOf(calendar, field));
    }

    public static final int DECADE = 5467;
    public static final int YEAR_IN_DECADE = DECADE + 1;

    /**
     * Returns a boolean indicating if the calendar is set to the start of a 
     * period  as defined by the
     * given field. This delegates to startOfDay, -Week, -Month, -Year as appropriate.
     * The calendar is unchanged.
     * 
     * @param calendar
     * @param field the period to adjust, allowed are Calendar.DAY_OF_MONTH, -.MONTH, 
     * -.WEEK and YEAR and CalendarUtils.DECADE.
     * @throws IllegalArgumentException if the field is not supported.
     */
    public static boolean isStartOf(Calendar calendar, int field) {
        switch (field) {
        case Calendar.DAY_OF_MONTH:
            return isStartOfDay(calendar);
        case Calendar.MONTH:
            return isStartOfMonth(calendar);
        case Calendar.WEEK_OF_YEAR:
            return isStartOfWeek(calendar);
        case Calendar.YEAR:
            return isStartOfYear(calendar);
        case DECADE:
            return isStartOfDecade(calendar);
        default:
            throw new IllegalArgumentException("unsupported field: "
                    + field);
        }
    }

    /**
     * Returns a boolean indicating if the given calendar represents the 
     * start of a day (in the calendar's time zone). The calendar is unchanged.
     * 
     * @param calendar the calendar to check.
     * 
     * @return true if the calendar's time is the start of the day,
     *   false otherwise.
     */
    public static boolean isStartOfDay(Calendar calendar) {
        Calendar temp = (Calendar) calendar.clone();
        temp.add(Calendar.MILLISECOND, -1);
        return temp.get(Calendar.DATE) != calendar.get(Calendar.DATE);
    }

    /**
     * Returns a boolean indicating if the given calendar represents the 
     * start of a month (in the calendar's time zone). Returns true, if the time is 
     * the start of the first day of the month, false otherwise. The calendar is unchanged.
     * 
     * @param calendar the calendar to check.
     * 
     * @return true if the calendar's time is the start of the first day of the month,
     *   false otherwise.
     */
    public static boolean isStartOfMonth(Calendar calendar) {
        Calendar temp = (Calendar) calendar.clone();
        temp.add(Calendar.MILLISECOND, -1);
        return temp.get(Calendar.MONTH) != calendar.get(Calendar.MONTH);
    }

    /**
     * Returns a boolean indicating if the given calendar represents the 
     * start of a month (in the calendar's time zone). Returns true, if the time is 
     * the start of the first day of the month, false otherwise. The calendar is unchanged.
     * 
     * @param calendar the calendar to check.
     * 
     * @return true if the calendar's time is the start of the first day of the month,
     *   false otherwise.
     */
    public static boolean isStartOfWeek(Calendar calendar) {
        Calendar temp = (Calendar) calendar.clone();
        temp.add(Calendar.MILLISECOND, -1);
        return temp.get(Calendar.WEEK_OF_YEAR) != calendar
                .get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * Returns a boolean indicating if the given calendar represents the 
     * start of a year (in the calendar's time zone). Returns true, if the time is 
     * the start of the first day of the year, false otherwise. The calendar is unchanged.
     * 
     * @param calendar the calendar to check.
     * 
     * @return true if the calendar's time is the start of the first day of the month,
     *   false otherwise.
     */
    public static boolean isStartOfYear(Calendar calendar) {
        Calendar temp = (Calendar) calendar.clone();
        temp.add(Calendar.MILLISECOND, -1);
        return temp.get(Calendar.YEAR) != calendar.get(Calendar.YEAR);
    }

    /**
     * Returns a boolean indicating if the given calendar represents the 
     * start of a decade (in the calendar's time zone). Returns true, if the time is 
     * the start of the first day of the decade, false otherwise. The calendar is unchanged.
     * 
     * @param calendar the calendar to check.
     * 
     * @return true if the calendar's time is the start of the first day of the month,
     *   false otherwise.
     */
    public static boolean isStartOfDecade(Calendar calendar) {
        Calendar temp = (Calendar) calendar.clone();
        temp.add(Calendar.MILLISECOND, -1);
        return decade(temp.get(Calendar.YEAR)) != decade(calendar
                .get(Calendar.YEAR));
    }

    /**
     * Increments the calendar field of the given calendar by amount. 
     * 
     * @param calendar
     * @param field the field to increment, allowed are all fields known to
     *   Calendar plus DECADE.
     * @param amount
     * 
     * @throws IllegalArgumentException
     */
    public static void add(Calendar calendar, int field, int amount) {
        if (isNativeField(field)) {
            calendar.add(field, amount);
        } else {
            switch (field) {
            case DECADE:
                calendar.add(Calendar.YEAR, amount * 10);
                break;
            default:
                throw new IllegalArgumentException("unsupported field: "
                        + field);
            }

        }
    }

    /**
     * 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);
        }
    }

    /**
     * @param year
     * @return
     */
    private static int decade(int year) {
        return (year / 10) * 10;
    }

    /**
     * @param calendarField
     * @return
     */
    private static boolean isNativeField(int calendarField) {
        return calendarField < DECADE;
    }
}

Related Tutorials