Java Calendar Create createCalendar(int year, int month, int day, int hour, int minute, int second, boolean ceiling)

Here you can find the source of createCalendar(int year, int month, int day, int hour, int minute, int second, boolean ceiling)

Description

Creates a calendar in the given year.

License

Open Source License

Declaration

public static Calendar createCalendar(int year, int month, int day,
        int hour, int minute, int second, boolean ceiling) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;

import java.util.TimeZone;

public class Main {
    /**// w  ww  .  j a va  2  s. com
     * Creates a calendar in the given year. Year must be specified, but all
     * other fields can be -1 if unknown. If -1, they're either the greatest of
     * least value of the calendar's current state depending on the value of
     * ceiling.
     */
    public static Calendar createCalendar(int year, int month, int day,
            int hour, int minute, int second, boolean ceiling) {
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        cal.set(Calendar.YEAR, year);
        fillInField(Calendar.MONTH, month - 1, ceiling, cal);
        fillInField(Calendar.DAY_OF_MONTH, day, ceiling, cal);
        fillInField(Calendar.HOUR_OF_DAY, hour, ceiling, cal);
        fillInField(Calendar.MINUTE, minute, ceiling, cal);
        fillInField(Calendar.SECOND, second, ceiling, cal);
        fillInField(Calendar.MILLISECOND, -1, ceiling, cal);
        return cal;
    }

    public static void fillInField(int field, int value, boolean ceiling,
            Calendar cal) {
        if (value >= 0) {
            cal.set(field, value);
        } else if (ceiling) {
            cal.set(field, cal.getActualMaximum(field));
        } else {
            cal.set(field, cal.getActualMinimum(field));
        }
    }
}

Related

  1. createCalendar(final int year, final int month, final int day)
  2. createCalendar(final int year, final int month, final int day, final int hour, final int minute, final int second)
  3. createCalendar(final int year, final int month, final int dayOfMonth, final TimeZone timeZone)
  4. createCalendar(int year, int month, int date)
  5. createCalendar(int year, int month, int day)
  6. createCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second)
  7. createCalendar(long timeInMillis)
  8. createCalendarFromParts(String[] parts)
  9. createCalendarInstance(final Locale locale)