|Tips on using the Calendar class
CIT 591, David Matuszek, Fall 2001
You need to use Java's
Calendar class. When you write your program,
do not name your own class
Calendar--that would make
it very difficult to use Java's
The way you use a
Calendar is like this:
For reasons we have not yet discussed in class, you cannot create a
object directly; instead, you must create a
which can then be assigned to a
Calendar variable. Like so:
Calendar calendar = new GregorianCalendar();
(I apologize for not having noticed this particular problem earlier.)
The easiest way to set a calendar to a particular date is to use the following
instance method of the
void set(int year, int month, int date)
Note that the actual parameters are all integers. This makes sense for the year, and for the "date" (an integer between 1 and 31), but what about the month? It could be an integer between 0 and 11, or it could be an integer between 1 and 12. Which way does Java do it?
You don't actually have to know what numbers Java uses for the months, because
it provides you with a set of named constants
Calendar.FEBRUARY, and so on up through
(There's even a named constant for a month you probably don't know about,
set method allows you to set your calendar to some particular
day, for example, January 1 of the year that is entered by the user. The next
step is to ask your calendar for some information about that date, such as the
day of the week on which it falls.
If you understand the idea of named constants, the next routine,
should make sense. If you do not, you will find it doubly confusing, because
it uses a named constant to tell it what to get, and it returns an integer.
In some cases, you need to compare the returned integer to other named constants
in order to make sense of it.
The form of the get method is:
int get(int field)
where the actual parameter is an integer telling what information you want
to get. A actual parameter of
Calendar.YEAR would be asking for
the year, an actual parameter of
Calendar.DAY_OF_WEEK would be
asking for the day of the week,
Calendar.WEEK_OF_MONTH would ask
for which week of the month the day in in, and so on. You are probably most
Calendar.DAY_OF_WEEK, which will return an integer
between 1 and 7--or is it between 0 and 6? Once again, you don't have to remember,
because you can compare the returned value to the named constants
Calendar.MONDAY, and so on. Or you can look at the
documentation and find out which integers are used for which days of the week.
Another method you might find useful is
int getActualMaximum(int field)
but I'll leave you to read the documentation for that method.
Again, these methods are all instance methods that are defined for you in the
Calendar class. You do not write these methods yourself,
you just use them.
A final note: If you read the assignment, you will see that I am asking you to get the year from the BlueJ dialog box. This means that your program has to work for any year that the user types in to this dialog box. I thought this was clear from the assignment, but apparently it isn't clear to everyone.