Java The Complete Reference, Seventh Edition

(Greg DeLong) #1
Sample output is shown here:

Mon Jan 01 16:28:16 CST 2007
Milliseconds since Jan. 1, 1970 GMT = 1167690496023

Calendar


The abstractCalendarclass provides a set of methods that allows you to convert a time in
milliseconds to a number of useful components. Some examples of the type of information
that can be provided are year, month, day, hour, minute, and second. It is intended that
subclasses ofCalendarwill provide the specific functionality to interpret time information
according to their own rules. This is one aspect of the Java class library that enables you
to write programs that can operate in international environments. An example of such a
subclass isGregorianCalendar.
Calendarprovides no public constructors.
Calendardefines several protected instance variables.areFieldsSetis abooleanthat
indicates if the time components have been set.fieldsis an array ofints that holds the
components of the time.isSetis abooleanarray that indicates if a specific time component
has been set.timeis alongthat holds the current time for this object.isTimeSetis aboolean
that indicates if the current time has been set.
Some commonly used methods defined byCalendarare shown in Table 18-4.

Chapter 18: java.util Part 2: More Utility Classes 509


Method Description
abstract void add(intwhich, intval) Addsvalto the time or date component specified
bywhich.To subtract, add a negative value.which
must be one of the fields defined byCalendar, such
asCalendar.HOUR.
boolean after(ObjectcalendarObj) Returnstrueif the invokingCalendarobject
contains a date that is later than the one specified
bycalendarObj.Other wise, it returnsfalse.
boolean before(ObjectcalendarObj) Returnstrueif the invokingCalendarobject contains
a date that is earlier than the one specified by
calendarObj.Other wise, it returnsfalse.
final void clear( ) Zeros all time components in the invoking object.
final void clear(intwhich) Zeros the time component specified bywhichin
the invoking object.
Object clone( ) Returns a duplicate of the invoking object.
boolean equals(ObjectcalendarObj) Returnstrueif the invokingCalendarobject
contains a date that is equal to the one specified
bycalendarObj.Other wise, it returnsfalse.

TABLE 18-4 Commonly Used Methods Defined byCalendar
Free download pdf