• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

whats wrong with this code?? number of days in month..

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am just writing some code to find the number of days in any month..
for testing, i have this code:



This code Runs perfect for every Month EXCEPT for April, where it gives 29 days instead of 30 ??? any clue why??

i was just testing by
commenting

and including


where monthNum varies from 0 to 11. It is giving trouble only with
cal.set(2005,3,1);

what has APRIL month done wrong???

Your help would be appreciated..

Another question, which is not regarding the previous one..

Calendar.DATE does not give 22, but gives 5, why??
I have other ways of getting today's date, but just curious to know what's wrong with Calendar.DATE??

Thanks again ;-)
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These are constants defined in API.

Calendar.DATE
Calendar.MONTH
Calendar.YEAR

They do not refer to today's date, month, or year. (so Calendar.DATE happens to = 5, not 22 <--- today's date)

They are used by the Calendar class for example when you want to use the add() method you use one of those constants for the field parameter (as you did in your cod):

add(int field, int amount)
Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.


When you use "cal.set(Calendar.YEAR,Calendar.MONTH-1,1);" in your code it isn't probably doing what you think.

 
Carol Enderlin
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Google is pretty helpful. I searched for "java days in month"...

From Java Developer's Almanac Example

 
Jack Daniel
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Carol,..

That makes everything clear to me...
learnt something new...

 
Jack Daniel
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the mean time, i have written this, though there is not much of API !!

 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rules for leap year are a bit trickier than % 4 if you have to support centuries past and future.

Another approach to days in month is just an array indexed by month 0..11

I made one program that only had to operate on a range of 20 years or so. I made an array of 20 booleans and indexed by year minus the first year. Saved two or three CPU cycles on the modulo divide.
 
Jack Daniel
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stan,

you are correct..

I have seen the algorithm on how to find if an year is a leap year or not..

So, I made the change



thx
 
reply
    Bookmark Topic Watch Topic
  • New Topic