Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
www - 15 Jul 2007 20:26 GMT
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:


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.