Calendar
The abstract calendar class provides a set
of methods that allows to convert a time in milliseconds to number of useful
components. The type of information that can be provide are:
Year,Month,Day,Hour,Minute and second.
Calendar
provides no public constructors. Instead,
getInstance() method is used to obtain a calendar initialized to the current
date and time.
Calendar defines several protected instance
variables are Fieldset is a boolean that indicates if the time components have
been set. Fields is an array of that holds the components of the time isSet is
a boolean array that indicates if a specific time component has been set. Time
is a long that holds the current time for this object isTimeSet is a boolean
that indicates if the current time has been set.
Calendar defines the following int constants,
which are used when you get or set
Components of the
calendar:
AM
|
NOVEMBER
|
PM
|
DECEMBER
|
AM_PM
|
DATE
|
SUNDAY
|
DAY_OF_MONTH
|
MONDAY
|
DAY_OF_WEEK
|
TUESDAY
|
DAY_OF_WEEK_IN_MONTH
|
WEDNESDAY
|
DAY_OF_YEAR
|
THURSDAY
|
MONTH
|
FRIDAY
|
WEEK_OF_MONTH
|
SATURDAY
|
WEEK_OF_YEAR
|
JANUARY
|
HOUR
|
FEBRUARY
|
MINUTE
|
MARCH
|
SECOND
|
APRIL
|
MILLISECOND
|
MAY
|
HOUR_OF_DAY
|
JUNE
|
YEAR
|
JULY
|
ZONE_OFFSET
|
AUGUST
|
FIELD_COUNT
|
SEPTMBER
|
DST_OFFSET
|
OCTOBER
|
ERA
|
Methods:
Static calendar
getInstance()
Returns a calendar object for the default
locale and time Zone.
abstract void add(int
which,int val)
Adds val to the time or date components
specified by which to subtract, add a negative value which must be one of the
fields defined by calendar such as calendar. HOUR
boolean after(object
calendar obj)
Returns true if the invoking calendar
object contains a date that is later than the once specified by calendar obj.
otherwise , it returns false.
boolean before(object
calendar obj)
Returns true if the invoking calendar
object contains a date that is before than the once specified by calendar obj.
otherwise , it returns false.
final void clear()
Zeros
all time components in the invoking object.
boolean equals(object
calenderObj)
Returns true if the invoking calendar
object contains a date that is equal to the one specified by calendarObj.
Otherwise it returns false.
final int get(int
calendar Field)
Returns the value of one component of the
invoking object. The component is indicated by calendarField. Examples of the
components that can be requested are calendar. Year,calendar. Month,calendar.
Minute and so fourth.
TimeZone getTimeZone()
Returns
the timeZone for the invoking object.
Final void set (int
year, int month, int dayOfMonth, int hours, int minutes, int seconds)
Sets various date and time components of the invoking object.
void
setTimeZone(TimeZone tz)
Sets the time zone for the invoking object
to that specified by tz.
Gregorian Calendar
Gregorian
calendar is a concrete implementation of a calendar. The getInstance() method
of calendar returns a GregorianCalendar. initialized with the current date and
time in the default locale and time zone.
Gregorian
calendar defines two fields: AD and BC. These represent the two defined by the
Gregorian calendar.
There
are also several constructors for GregorianCalendar objects.
GregorianCalendar()
The default, initializes the object with
the current date and time in the default locale and time zone. Three
constructors offer increasing levels of specificity.
GregorianCalendar(int
Year, int month,int dayofMonth)
GregorianCalendar(int
year,int month,int dayofMonth)
GregorianCalendar(int
year,int month,int dayofMonth,int hours,int minutes,int seconds)
All three versions set the day,month and
year.
Here, year specifies the number of years
that have elapsed since 1900.the month is specified by month with zero indicating january.The day
of month is specified by day of month.
The first version sets the time to
midnight.The second version also sets the hours and the minutes.
The third version adds second.
The
following constructors create objects initialized with the current date and
time using specified time zone and/or locale.
GregorianCalendar (Locale locale)
GregorianCalendar (Timezone timezone)
GregorianCalendar (Timezone timezone,Locale
locale)
GregorianCalendar provides an
implementation of all the abstract methods in calendar.
Method
Boolean isLeapyear(int
year)
This method returns true if year is a
leapyear and otherwise returns false.
Posted By : Ruchita APnday