FileDocCategorySizeDatePackage
Calendar.javaAPI DocphoneME MR2 API (J2ME)19027Wed May 02 17:59:56 BST 2007java.util

Calendar

public abstract class Calendar extends Object
Calendar is an abstract base class for converting between a Date object and a set of integer fields such as YEAR, MONTH, DAY, HOUR, and so on. (A Date object represents a specific instant in time with millisecond precision. See {@link Date} for information about the Date class.)

Subclasses of Calendar interpret a Date according to the rules of a specific calendar system.

Like other locale-sensitive classes, Calendar provides a class method, getInstance, for getting a generally useful object of this type.

Calendar rightNow = Calendar.getInstance();

A Calendar object can produce all the time field values needed to implement the date-time formatting for a particular language and calendar style (for example, Japanese-Gregorian, Japanese-Traditional).

When computing a Date from time fields, there may be insufficient information to compute the Date (such as only year and month but no day in the month).

Insufficient information. The calendar will use default information to specify the missing fields. This may vary by calendar; for the Gregorian calendar, the default for a field is the same as that of the start of the epoch: i.e., YEAR = 1970, MONTH = JANUARY, DATE = 1, etc. Note: The ambiguity in interpretation of what day midnight belongs to, is resolved as so: midnight "belongs" to the following day.
23:59 on Dec 31, 1969 < 00:00 on Jan 1, 1970.
12:00 PM is midday, and 12:00 AM is midnight.
11:59 PM on Jan 1 < 12:00 AM on Jan 2 < 12:01 AM on Jan 2.
11:59 AM on Mar 10 < 12:00 PM on Mar 10 < 12:01 PM on Mar 10.
24:00 or greater are invalid. Hours greater than 12 are invalid in AM/PM mode. Setting the time will never change the date.

If equivalent times are entered in AM/PM or 24 hour mode, equality will be determined by the actual time rather than the entered time.

This class has been subset for J2ME based on the JDK 1.3 Calendar class. Many methods and variables have been pruned, and other methods simplified, in an effort to reduce the size of this class.

see
java.util.Date
see
java.util.TimeZone
version
CLDC 1.1 02/01/2002 (based on JDK 1.3)

Fields Summary
public static final int
YEAR
Field number for get and set indicating the year. This is a calendar-specific value.
public static final int
MONTH
Field number for get and set indicating the month. This is a calendar-specific value.
public static final int
DATE
Field number for get and set indicating the day of the month. This is a synonym for DAY_OF_MONTH.
public static final int
DAY_OF_MONTH
Field number for get and set indicating the day of the month. This is a synonym for DATE.
public static final int
DAY_OF_WEEK
Field number for get and set indicating the day of the week.
public static final int
AM_PM
Field number for get and set indicating whether the HOUR is before or after noon. E.g., at 10:04:15.250 PM the AM_PM is PM.
public static final int
HOUR
Field number for get and set indicating the hour of the morning or afternoon. HOUR is used for the 12-hour clock. E.g., at 10:04:15.250 PM the HOUR is 10.
public static final int
HOUR_OF_DAY
Field number for get and set indicating the hour of the day. HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the HOUR_OF_DAY is 22.
public static final int
MINUTE
Field number for get and set indicating the minute within the hour. E.g., at 10:04:15.250 PM the MINUTE is 4.
public static final int
SECOND
Field number for get and set indicating the second within the minute. E.g., at 10:04:15.250 PM the SECOND is 15.
public static final int
MILLISECOND
Field number for get and set indicating the millisecond within the second. E.g., at 10:04:15.250 PM the MILLISECOND is 250.
public static final int
SUNDAY
Value of the DAY_OF_WEEK field indicating Sunday.
public static final int
MONDAY
Value of the DAY_OF_WEEK field indicating Monday.
public static final int
TUESDAY
Value of the DAY_OF_WEEK field indicating Tuesday.
public static final int
WEDNESDAY
Value of the DAY_OF_WEEK field indicating Wednesday.
public static final int
THURSDAY
Value of the DAY_OF_WEEK field indicating Thursday.
public static final int
FRIDAY
Value of the DAY_OF_WEEK field indicating Friday.
public static final int
SATURDAY
Value of the DAY_OF_WEEK field indicating Saturday.
public static final int
JANUARY
Value of the MONTH field indicating the first month of the year.
public static final int
FEBRUARY
Value of the MONTH field indicating the second month of the year.
public static final int
MARCH
Value of the MONTH field indicating the third month of the year.
public static final int
APRIL
Value of the MONTH field indicating the fourth month of the year.
public static final int
MAY
Value of the MONTH field indicating the fifth month of the year.
public static final int
JUNE
Value of the MONTH field indicating the sixth month of the year.
public static final int
JULY
Value of the MONTH field indicating the seventh month of the year.
public static final int
AUGUST
Value of the MONTH field indicating the eighth month of the year.
public static final int
SEPTEMBER
Value of the MONTH field indicating the ninth month of the year.
public static final int
OCTOBER
Value of the MONTH field indicating the tenth month of the year.
public static final int
NOVEMBER
Value of the MONTH field indicating the eleventh month of the year.
public static final int
DECEMBER
Value of the MONTH field indicating the twelfth month of the year.
public static final int
AM
Value of the AM_PM field indicating the period of the day from midnight to just before noon.
public static final int
PM
Value of the AM_PM field indicating the period of the day from noon to just before midnight.
private static final int
FIELDS
protected int[]
fields
The field values for the currently set time for this calendar.
protected boolean[]
isSet
The flags which tell if a specified time field for the calendar is set. This is an array of FIELD_COUNT booleans,
protected long
time
The currently set time for this calendar, expressed in milliseconds after January 1, 1970, 0:00:00 GMT.
private boolean
isTimeSet
True if then the value of time is valid. The time is made invalid by a change to an item of field[].
private TimeZone
zone
The TimeZone used by this calendar. Calendar uses the time zone data to translate between the current/default system time and GMT time.
private Date
dateObj
Constructors Summary
protected Calendar()
Constructs a Calendar with the default time zone.

see
TimeZone#getDefault


                       
      
        fields = new int[FIELDS];
        isSet = new boolean[FIELDS];

        zone = TimeZone.getDefault();
        if (zone == null) {
            throw new RuntimeException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       "Could not find default timezone"
/* #endif */
            );
        }
        setTimeInMillis(System.currentTimeMillis());
    
Methods Summary
public booleanafter(java.lang.Object when)
Compares the time field records. Equivalent to comparing result of conversion to UTC.

param
when the Calendar to be compared with this Calendar.
return
true if the current time of this Calendar is after the time of Calendar when; false otherwise.

        return (when instanceof Calendar
                && getTimeInMillis() > ((Calendar)when).getTimeInMillis());
    
public booleanbefore(java.lang.Object when)
Compares the time field records. Equivalent to comparing result of conversion to UTC.

param
when the Calendar to be compared with this Calendar.
return
true if the current time of this Calendar is before the time of Calendar when; false otherwise.

        return (when instanceof Calendar
                && getTimeInMillis() < ((Calendar)when).getTimeInMillis());
    
protected abstract voidcomputeFields()
Converts the current millisecond time value time to field values in fields[]. This allows you to sync up the time field values with a new time that is set for the calendar.

protected abstract voidcomputeTime()
Converts the current field values in fields[] to the millisecond time value time.

public booleanequals(java.lang.Object obj)
Compares this calendar to the specified object. The result is true if and only if the argument is not null and is a Calendar object that represents the same calendar as this object.

param
obj the object to compare with.
return
true if the objects are the same; false otherwise.

        if (this == obj) {
            return true;
        }

        if (!(obj instanceof Calendar)) {
            return false;
        }

        Calendar that = (Calendar)obj;
        return getTimeInMillis() == that.getTimeInMillis() && zone.equals(that.zone);
    
public final intget(int field)
Gets the value for a given time field.

param
field the given time field (either YEAR, MONTH, DATE, DAY_OF_WEEK, HOUR_OF_DAY, HOUR, AM_PM, MINUTE, SECOND, or MILLISECOND
return
the value for the given time field.
exception
ArrayIndexOutOfBoundsException if the parameter is not one of the above.

        if ( field == DAY_OF_WEEK ||
             field == HOUR_OF_DAY ||
             field == AM_PM ||
             field == HOUR ) {
            getTimeInMillis();
            computeFields();
        }
        return this.fields[field];
    
public static synchronized java.util.CalendargetInstance()
Gets a calendar using the default time zone.

return
a Calendar.

        try {
            // Obtain the calendar implementation class
            Class clazz = Class.forName("com.sun.cldc.util.j2me.CalendarImpl");

            // Construct a new instance
            return (Calendar)clazz.newInstance();
        }
        catch (Exception x) {}
        return null;
    
public static synchronized java.util.CalendargetInstance(java.util.TimeZone zone)
Gets a calendar using the specified time zone.

param
zone the time zone to use
return
a Calendar.

        Calendar cal = getInstance();
        cal.setTimeZone(zone);
        return cal;
    
public final java.util.DategetTime()
Gets this Calendar's current time.

return
the current time.
see
#setTime

        if (dateObj == null) {
            return dateObj = new Date( getTimeInMillis() );
        } else {
            synchronized (dateObj) {
                dateObj.setTime( getTimeInMillis() );
                return dateObj;
            }
        }
    
protected longgetTimeInMillis()
Gets this Calendar's current time as a long expressed in milliseconds after January 1, 1970, 0:00:00 GMT (the epoch).

return
the current time as UTC milliseconds from the epoch.
see
#setTimeInMillis

        if (!isTimeSet) {
            computeTime();
            isTimeSet = true;
        }
        return this.time;
    
public java.util.TimeZonegetTimeZone()
Gets the time zone.

return
the time zone object associated with this calendar.
see
#setTimeZone

        return zone;
    
public final voidset(int field, int value)
Sets the time field with the given value.

param
field the given time field.
param
value the value to be set for the given time field.
exception
ArrayIndexOutOfBoundsException if an illegal field parameter is received.

        isTimeSet = false;

        this.isSet[field] = true;
        this.fields[field] = value;
    
public final voidsetTime(java.util.Date date)
Sets this Calendar's current time with the given Date.

Note: Calling setTime() with Date(Long.MAX_VALUE) or Date(Long.MIN_VALUE) may yield incorrect field values from get().

param
date the given Date.
see
#getTime

        setTimeInMillis( date.getTime() );
    
protected voidsetTimeInMillis(long millis)
Sets this Calendar's current time from the given long value.

param
millis the new time in UTC milliseconds from the epoch.
see
#getTimeInMillis

        isTimeSet = true;
        this.fields[DAY_OF_WEEK] = 0;
        this.time = millis;
        computeFields();
    
public voidsetTimeZone(java.util.TimeZone value)
Sets the time zone with the given time zone value.

param
value the given time zone.
see
#getTimeZone

        zone = value;
        getTimeInMillis();
        computeFields();