Class 1, Part 2 |
Intro to Java 30-IT-396 |
|
Methods of the GregorianCalendar
-
Many Java objects have getter and setter methods to obtain or change class
data.
-
For instance, getGallonsOfGas() is a standard way to get the value
of the gallonsOfGas attribute. Yes, the capitalization you
see is proper.
-
On the other hand, setGallonsOfGas(int gallonsOfGas) is the proper
method to set the gallonsOfGas attribute.
-
Curiously, the GregorianCalendar object acts in a similar, yet
not identical way. You have to use contstants of the
GregorianCalendar
class to get or set needed attributes. A full list of these constants
is available on the API
doc.
-
The following code gets a GregorianCalendar object, gets the hour, adds
two to it, and then sets the hour.
GregorianCalendar expire = new GregorianCalendar();
int hour = expire.get(Calendar.HOUR);
hour += 2;
expire.set(Calendar.HOUR, hour);
-
The above demonstrates the user of the get() and set() methods, along with
constants. However, if you really needed to add two hours to the
time, you can simply use the add() method of the GregorianCalendar object.
expire.add(Calendar.HOUR, 2);
-
To subtract two hours from the given time, simply use -2.
getTime() and setTime(Date date) methods


Created by: Brandan
Jones January 4, 2002