Java Calendar Create createCalendar()

Here you can find the source of createCalendar()

Description

This method creates and returns a new calendar.

License

Apache License

Return

A new calendar.

Declaration

public static GregorianCalendar createCalendar() 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import static java.util.Calendar.HOUR_OF_DAY;
import static java.util.Calendar.MILLISECOND;
import static java.util.Calendar.MINUTE;

import static java.util.Calendar.SECOND;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    /**/* ww w . j av a2 s.  c om*/
     * This method creates and returns a new calendar.
     *
     * @return A new calendar.
     */
    public static GregorianCalendar createCalendar() {
        return createCalendar(null);
    }

    /**
     * This method creates and returns a new calendar.
     *
     * @param date The date to initialize the calendar to.
     *
     * @return A new calendar.
     */
    public static GregorianCalendar createCalendar(Date date) {
        GregorianCalendar calendar = new GregorianCalendar();

        if (date == null) {
            date = new Date();
        }

        calendar.setTime(date);

        clearClockFor(calendar);

        return calendar;
    }

    /**
     * This method sets the clock to 00:00:00:000.
     *
     * @param calendar The calendar to clear the clock for.
     */
    public static void clearClockFor(Calendar calendar) {
        // Set time to 00:00:00:000
        calendar.set(HOUR_OF_DAY, 0);
        calendar.set(MILLISECOND, 0);
        calendar.set(MINUTE, 0);
        calendar.set(SECOND, 0);
    }
}

Related

  1. createCalendar()
  2. createCalendar()
  3. createCalendar()
  4. createCalendar()
  5. createCalendar(Date date)
  6. createCalendar(final int year, final int month, final int day)