For GregorianCalendar, why isLeapYear(int year)? I think shoud beisLeapYear()!

W

www

Hi,

In GregorianCalendar, I don't understand why isLeapYear method needs an
argument. e.g.

Calendar cal = new GregorianCalendar();
cal.set(bla.. bla .. bla); //set time

//check if the time represent by cal is a leap year
if(cal.isLeapYear()) { ... } //oops, wrong, need to be

if(cal.isLeapYear(cal.get(Calendar.YEAR))) { ... } //!!! I don't
understand, why need "cal" twice to check if cal is in a leap year

Thank you for your help.
 
E

Eric Sosman

www said:
Hi,

In GregorianCalendar, I don't understand why isLeapYear method needs an
argument. e.g.

Calendar cal = new GregorianCalendar();
cal.set(bla.. bla .. bla); //set time

//check if the time represent by cal is a leap year
if(cal.isLeapYear()) { ... } //oops, wrong, need to be

if(cal.isLeapYear(cal.get(Calendar.YEAR))) { ... } //!!! I don't
understand, why need "cal" twice to check if cal is in a leap year

You may be surprised to learn that the code you have
posted won't compile, even when placed in a suitable context
and with its `...' fleshed out. The Calendar class has no
isLeapYear() method at all, so `cal' cannot invoke it.

As for the isLeapYear() method of GregorianCalendar,
sometimes you want to decide whether the calendar's current
year is or isn't a leap year -- that's what you've tried
to show -- but sometimes you want to ask the same question
about some other year. In that case, you get to choose
between

GregorianCalendar cal = new GregorianCalendar();
if (cal.isLeapYear(2000)) ... // as things stand

and

GregorianCalendar cal = new GregorianCalendar();
cal.set(Calendar.YEAR, 2000);
if (cal.isLeapYear()) ... // fictional method

or

GregorianCalendar cal = new GregorianCalendar(
2000, Calendar.JANUARY, 1);
if (cal.isLeapYear()) ... // fictional method
 
R

Roedy Green

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top