Java Calendar Create getCalendar()

Here you can find the source of getCalendar()

Description

Get a calendar for the default timezone.

License

Mozilla Public License

Return

a calendar instance. A cached instance is returned where possible

Declaration

private static Calendar getCalendar() 

Method Source Code

//package com.java2s;
/*//from www .ja  v  a  2  s  .  c  om
 * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, and the
 * EPL 1.0 (http://h2database.com/html/license.html). Initial Developer: H2
 * Group Iso8601: Initial Developer: Robert Rathsack (firstName dot lastName at
 * gmx dot de)
 */

import java.util.Calendar;

import java.util.TimeZone;

public class Main {
    /**
     * The thread local. Can not override initialValue because this would result
     * in an inner class, which would not be garbage collected in a web
     * container, and prevent the class loader of H2 from being garbage
     * collected. Using a ThreadLocal on a system class like Calendar does not
     * have that problem, and while it is still a small memory leak, it is not a
     * class loader memory leak.
     */
    private static final ThreadLocal<Calendar> CACHED_CALENDAR = new ThreadLocal<Calendar>();
    /**
     * A cached instance of Calendar used when a timezone is specified.
     */
    private static final ThreadLocal<Calendar> CACHED_CALENDAR_NON_DEFAULT_TIMEZONE = new ThreadLocal<Calendar>();

    /**
     * Get a calendar for the default timezone.
     *
     * @return a calendar instance. A cached instance is returned where possible
     */
    private static Calendar getCalendar() {
        Calendar c = CACHED_CALENDAR.get();
        if (c == null) {
            c = Calendar.getInstance();
            CACHED_CALENDAR.set(c);
        }
        c.clear();
        return c;
    }

    /**
     * Get a calendar for the given timezone.
     *
     * @param tz timezone for the calendar, is never null
     * @return a calendar instance. A cached instance is returned where possible
     */
    private static Calendar getCalendar(TimeZone tz) {
        Calendar c = CACHED_CALENDAR_NON_DEFAULT_TIMEZONE.get();
        if (c == null || !c.getTimeZone().equals(tz)) {
            c = Calendar.getInstance(tz);
            CACHED_CALENDAR_NON_DEFAULT_TIMEZONE.set(c);
        }
        c.clear();
        return c;
    }
}

Related

  1. getCalendar()
  2. getCalendar()
  3. getCalendar()
  4. getCalendar()
  5. getCalendar()
  6. getCalendar(boolean skipConversion)
  7. getCalendar(boolean skipConversion)
  8. getCalendar(Calendar cal, int relativeDay)
  9. getCalendar(final Date dateAndTime)