Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH. - Java java.util

Java examples for java.util:Month

Description

Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.

Demo Code


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

public class Main {
    public static void main(String[] argv) throws Exception {
        Calendar calendar = Calendar.getInstance();
        int year = 2;
        int month = 2;
        int date = 2;
        System.out.println(set(calendar, year, month, date));
    }//from  ww  w  . j  a v  a 2 s .  c  om

    /**
     * Sets the values for the calendar fields <code>YEAR</code>,
     * <code>MONTH</code>, and <code>DAY_OF_MONTH</code>. Previous values of
     * other calendar fields are retained. If this is not desired, call
     * {@link #clear()} first.
     * 
     * @param calendar calendar to set
     * @param year the value used to set the <code>YEAR</code> calendar field.
     * @param month the value used to set the <code>MONTH</code> calendar field.
     *            Month value is 0-based. e.g., 0 for January.
     * @param date the value used to set the <code>DAY_OF_MONTH</code> calendar
     *            field.
     * @return boolean check
     */
    public static Boolean set(Calendar calendar, int year, int month,
            int date) {

        Boolean isCalendarValide = null;

        if (calendar == null) {
            isCalendarValide = false;
            throw new NullPointerException("calendar null");
        }

        calendar.clear();

        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DATE, date);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        isCalendarValide = true;

        return isCalendarValide;
    }
}

Related Tutorials