Class Calendar


  extended by Object
      extended by Calendar

public class Calendar
extends Object

Calendar is a class for getting and setting dates using a set of integer fields such as YEAR, MONTH, DAY.

The fields are accessed directly in the code without a prefix:

   Calendar cal = new Calendar();
   // using the operator get [] and a constant field name
   int today = cal[DAY_OF_WEEK]; 
 

Calendar data can be accessed using the in-built operators get and set with brackets.

The following constant fields from API are used with Calendar:

The following constant fields from API are used with Calendar for formatting:

See Also:
Operators, operator get, operator set

Constructor Summary
Calendar()
          Creates a calendar using the default time zone and locale.
Calendar(TimeZone timeZone)
          Creates a calendar using the time zone given as argument and default locale.
 
Method Summary
 boolean after(Calendar when)
          Compares the time field records.
 boolean before(Calendar when)
          Compares the time field records.
 Calendar copy()
          Creates a copy from this calendar instance.
 String format(int format)
          Formats calendars date and/or time as string according to current locale/language.
 long getMillis()
          Gets this Calendar's current time as a long expressed in milliseconds after January 1, 1970, 0:00:00 GMT (the epoch).
 TimeZone getTimeZone()
          Gets the TimeZone for this Calendar object.
 int operator_get(int field)
          Gets the value for a given time field.
 void operator_set(int field, int value)
          Sets the time field with the given value.
 void set(int year, int month, int date)
          Sets the values for the fields year, month, and date.
 void set(int year, int month, int date, int hour, int minute)
          Sets the values for the fields year, month, date, hour, and minute.
 void set(int year, int month, int date, int hour, int minute, int second)
          Sets the values for the fields year, month, date, hour, minute, and second.
 void setMillis(long millis)
          Sets this Calendar's current time from the given long value.
 void setTimeZone(TimeZone timeZone)
          Sets the TimeZone for this Calendar object.
 
Methods inherited from class Object
toString, equals, hashCode
 
Methods inherited from
equals, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Calendar

public Calendar()
Creates a calendar using the default time zone and locale. The Calendar is based on the current time in the default time zone with the default locale.


Calendar

public Calendar(TimeZone timeZone)
Creates a calendar using the time zone given as argument and default locale.

Parameters:
timeZone - time zone
Method Detail

operator_get

public int operator_get(int field)
Gets the value for a given time field.

Parameters:
field - the given time field.
Returns:
the value for the given time field.

operator_set

public void operator_set(int field,
                         int value)
Sets the time field with the given value.

Parameters:
field - the given time field.
value - the value to be set for the given time field.

after

public boolean after(Calendar when)
Compares the time field records. Equivalent to comparing result of conversion to UTC.

Parameters:
when - the Calendar to be compared with this Calendar.
Returns:
true if the current time of this Calendar is after the time of Calendar when; false otherwise.

before

public boolean before(Calendar when)
Compares the time field records. Equivalent to comparing result of conversion to UTC.

Parameters:
when - the Calendar to be compared with this Calendar.
Returns:
true if the current time of this Calendar is before the time of Calendar when; false otherwise.

set

public void set(int year,
                int month,
                int date)
Sets the values for the fields year, month, and date. Previous values of other fields are retained. If this is not desired, call clear first.

Parameters:
year - the value used to set the YEAR time field.
month - the value used to set the MONTH time field. Month value is 0-based. e.g., 0 for January.
date - the value used to set the DATE time field.

set

public void set(int year,
                int month,
                int date,
                int hour,
                int minute)
Sets the values for the fields year, month, date, hour, and minute. Previous values of other fields are retained. If this is not desired, call clear first.

Parameters:
year - the value used to set the YEAR time field.
month - the value used to set the MONTH time field. Month value is 0-based. e.g., 0 for January.
date - the value used to set the DATE time field.
hour - the value used to set the HOUR_OF_DAY time field.
minute - the value used to set the MINUTE time field.

set

public void set(int year,
                int month,
                int date,
                int hour,
                int minute,
                int second)
Sets the values for the fields year, month, date, hour, minute, and second. Previous values of other fields are retained. If this is not desired, call clear first.

Parameters:
year - the value used to set the YEAR time field.
month - the value used to set the MONTH time field. Month value is 0-based. e.g., 0 for January.
date - the value used to set the DATE time field.
hour - the value used to set the HOUR_OF_DAY time field.
minute - the value used to set the MINUTE time field.
second - the value used to set the SECOND time field.

getMillis

public long getMillis()
Gets this Calendar's current time as a long expressed in milliseconds after January 1, 1970, 0:00:00 GMT (the epoch).

Returns:
the current time as UTC milliseconds from the epoch.
See Also:
setMillis(long)

setMillis

public void setMillis(long millis)
Sets this Calendar's current time from the given long value.

Parameters:
millis - the new time in UTC milliseconds from the epoch.
See Also:
getMillis()

format

public String format(int format)
Formats calendars date and/or time as string according to current locale/language.

Parameters:
format - FORMAT_DATE, FORMAT_TIME or FORMAT_DATETIME.

copy

public Calendar copy()
Creates a copy from this calendar instance.

Returns:
Duplicate calendar

getTimeZone

public TimeZone getTimeZone()
Gets the TimeZone for this Calendar object.

Returns:
time zone

setTimeZone

public void setTimeZone(TimeZone timeZone)
Sets the TimeZone for this Calendar object.

Parameters:
timeZone - new time zone