GeeCON Prague 2014*
The moose likes Java in General and the fly likes How can i calculate the number of days in year ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "How can i calculate the number of days in year ?" Watch "How can i calculate the number of days in year ?" New topic
Author

How can i calculate the number of days in year ?

Sudarshan Reddy
Greenhorn

Joined: Aug 23, 2007
Posts: 20
how can get the number of days in a year ?

ex: i need to calculate the number of days in a year day 2009 ?

please tell me


With Regards<br /> <br />Sudarshan Reddy
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42047
    
  64
I'm not sure what this has to do with JDBC -which is what this forum is all about- but the question seems to boil down to which year is a leap year, yes? Googling for "isleapyear java source" or something like that should provide you with the code.


Ping & DNS - my free Android networking tools app
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Calendar.getActualMaximum will help you out just fine.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
There are methods in the Calendar classes which can help; they will tell you 1st Feb is the 32nd day of the year.

A lot of people think the Jakarta Commons calendar classes or "Yoda" are better than the standard Java classes.
harilal ithikkat
Ranch Hand

Joined: Oct 06, 2008
Posts: 221
check whether the year is leap year or not
if( leap year)
366 days
else
365 days






SCJP 1.5
"A candle looses nothing by lighting another candle"
itechmentors.com
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
It is the GregorianCalendar class which has the method telling you whether it is a leap year.
It is easy enough to work out, which is what I think you are supposed to do:

  • Year number divides exactly by 400, OR
  • Year number divides exactly by 4 and doesn't divide exactly by 100.
  • So 1600, 1904, 1908, 2000, 2004 and 2008 were leap years, but 1900 wasn't.
    Vinoth Thirunavukarasu
    Ranch Hand

    Joined: Dec 18, 2008
    Posts: 164

    See this you can Find how its works and create of your own
    Link


    Java Best Practices
    Linux Best Practices
    Amortization Calculator

    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11356
        
      16

    Campbell Ritchie wrote:It is the GregorianCalendar class which has the method telling you whether it is a leap year.
    It is easy enough to work out, which is what I think you are supposed to do:

  • Year number divides exactly by 400, OR
  • Year number divides exactly by 4 and doesn't divide exactly by 100.
  • So 1600, 1904, 1908, 2000, 2004 and 2008 were leap years, but 1900 wasn't.

    That will work for modern years, but if you want to go back in history, it gets more complicated. 1752 only had 356 days (or maybe 354 - i'm not sure).


    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39084
        
      23
    It was, I think, 1585 which was 11 days short. But only in Britain; the rest of Europe had changed several years earlier! The date is in the GregorianCalendar documentation as 15th Oct 1582.
     
    GeeCON Prague 2014
     
    subject: How can i calculate the number of days in year ?