Java Reference
In-Depth Information
they were. You can reset all fields for a
GregorianCalendar
object to undefined by calling its
clear()
method.
The other versions of the
set()
method are:
set(int year, int month, int day, int hour, int minute)
set(int year, int month, int day, int hour, int minute, int second)
set(int field, int value)
It's obvious what the first two of these do. In each case the fields not explicitly set are left at their original
values. The third version of
set()
sets a field specified by one of the integer constants defined in the
Cal-
endar
class for this purpose (shown in
Table 15-2
)
:
TABLE 15-2
:
Calendar Field Setting Options
FIELD VALUE
AM_PM
Can have the values
AM
or
PM
, which correspond to values of 0 and 1
DAY_OF_WEEK
Can have the values
SUNDAY
,
MONDAY
, and so on, through to
SATURDAY
, which correspond to
values of 1 to 7
DAY_OF_WEEK_IN_MONTH
Ordinal number for the day of the week in the current month.
DAY_OF_YEAR
Can be set to a value from 1 to 366
MONTH
Can be set to a value of
JANUARY
,
FEBRUARY
, and so on, through to
DECEMBER
, corresponding
to values of 0 to 11
DAY_OF_MONTH
or
DATE
Can be set to a value from 1 to 31
WEEK_OF_MONTH
Can be set to a value from 1 to 6
Can be set to a value from 1 to 54
WEEK_OF_YEAR
A value from 0 to 23
HOUR_OF_DAY
A value from 1 to 12 representing the current hour in the a.m. or p.m.
HOUR
The current minute in the current hour — a value from 0 to 59
MINUTE
The second in the current minute, 0 to 59
SECOND
The millisecond in the current second, 0 to 999
MILLISECOND
The current year — for example, 2011
YEAR
Can be set to either
GregorianCalendar.BC
or
GregorianCalendar.AD
(both values being
defined in the
GregorianCalendar
class)
ERA
A millisecond value indicating the offset from GMT
ZONE_OFFSET
A millisecond value indicating the offset for daylight saving time in the current time zone
DST_OFFSET
Qualifying the names of these constants with the class name
GregorianCalendar
can make the code
look cumbersome but you can use static import for the constants to simplify things:
import static java.util.Calendar.*;
import static java.util.GregorianCalendar.*;
The static
import
statement imports only the names of static data members that are defined in a class,
not the names of inherited members. Therefore, you need two
import
statements if you want access to all
the constants you can use with the
GregorianCalendar
class.
With these two
import
statements in effect, you can write statements like this:
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(DAY_OF_WEEK, TUESDAY);