What does cal.get(7) do from a Calender instance?

729 views Asked by At

I am documenting some code and need help understanding this little line.

private Calendar cal = Calendar.getInstance();
if ((this.cal.get(7) != 7) || (this.cal.get(7) == 1)) {

What does cal.get(7) mean? I ran it on an IDE, and it gave me a result of 5. I tried cal.get(6), and got a result of 169.

2

There are 2 answers

1
aryn.galadar On BEST ANSWER

If "cal" is a java.util.Calendar, then 7 would be DAY_OF_WEEK. However, you shouldn't pass literal integers into the .get() method; use the constants on the Calendar class instead. So, for instance, this is the equivalent of your example:

if ((this.cal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) || (this.cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) {

(DAY_OF_YEAR has the value of 6, by the way)

The Calendar class has a large number of constants you can use; see the javadoc for more info.

0
Carx On
/**
     * Field number for <code>get</code> and <code>set</code> indicating the day
     * of the week.  This field takes values <code>SUNDAY</code>,
     * <code>MONDAY</code>, <code>TUESDAY</code>, <code>WEDNESDAY</code>,
     * <code>THURSDAY</code>, <code>FRIDAY</code>, and <code>SATURDAY</code>.
     *
     * @see #SUNDAY
     * @see #MONDAY
     * @see #TUESDAY
     * @see #WEDNESDAY
     * @see #THURSDAY
     * @see #FRIDAY
     * @see #SATURDAY
     */
    public final static int DAY_OF_WEEK = 7;