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.
Eric Sosman - 15 Jul 2007 21:14 GMT
> Hi,
>
[quoted text clipped - 9 lines]
> 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

Signature
Eric Sosman
esosman@ieee-dot-org.invalid
Roedy Green - 15 Jul 2007 22:19 GMT
>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 are right. isLeap() should be an instance method and isLeap(
yyyy) should be a static method. This is par for the course. The
folks who did Date and Calendar are not the best programmers.
This is the least of their problems. See
http://mindprod.com/jgloss/date.html
http://mindprod.com/jgloss/calendar.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Stefan Ram - 15 Jul 2007 22:28 GMT
>You are right. isLeap() should be an instance method and isLeap(
>yyyy) should be a static method.
I did it this way in my library:
The instance method:
http://www.purl.org/stefan_ram/html/ram.jar/de/dclj/ram/type/gregorian/Year.html
#isLeapYear()
The static method:
http://www.purl.org/stefan_ram/html/ram.jar/de/dclj/ram/algorithm/gregorian/Year
.html#isLeapYear(int)
The implementation of the instance method is based on the
static method: