views:

98

answers:

2

i get a date input from the user..in this case i need to determine if the year the user entered is a leap year or not ...and after the user has entered the entire date i.e the month date and year...i need to check if the date is in fact valid if yes need to determine the day for that particular date.. can some one tell me how this could be done..

+5  A: 
/* Returns true if the given year is a leap year */
static bool IsLeapYear(int year)
{
 if ((year % 400) == 0)
  return true;
 if ((year % 100) == 0)
  return false;
 if ((year % 4) == 0)
  return true;
 return false;
}


    2.6. What day of the week was 2 August 1953?
    --------------------------------------------

    To calculate the day on which a particular date falls, the following
    algorithm may be used (the divisions are integer divisions, in which
    remainders are discarded):

    a = (14 - month) / 12
    y = year - a
    m = month + 12*a - 2
    For Julian calendar: d = (5 + day + y + y/4 + (31*m)/12) mod 7
    For Gregorian calendar: d = (day + y + y/4 - y/100 + y/400 + (31*m)/12) mod 7

    The value of d is 0 for a Sunday, 1 for a Monday, 2 for a Tuesday, etc.

    Example: On what day of the week was the author born?

    My birthday is 2 August 1953 (Gregorian, of course).

    a = (14 - 8) / 12 = 0
    y = 1953 - 0 = 1953
    m = 8 + 12*0 - 2 = 6
    d = (2 + 1953 + 1953/4 - 1953/100 + 1953/400 + (31*6)/12) mod 7
      = (2 + 1953 +  488   -    19    +     4    +    15    ) mod 7
      = 2443 mod 7
      = 0

    I was born on a Sunday.


Read the Calendar FAQ for more information.

Andreas Bonini
is there a easy way of doing it using the calendar class (blackberry) and also how do we check if a entered date is valid or not ..again for blackberry..
Kaddy
A: 

ok the leap year part is clear..but how to check if the date is valid..i.e the month the date and the year together are valid or not...and get the day for that date...i.e. weather it was a sun mon ?

Kaddy
this is specifically for blackberry so is there a ways of using the blackberry calendar class instead of implementing out own logic for the day...?
Kaddy