2009-12-18 41 views
1

我從用戶得到一個日期輸入..在這種情況下,我需要確定用戶輸入的年份是否是閏年...和在用戶輸入完整的日期後,即月份日期和年份...我需要檢查日期是否實際上有效,如果是需要確定該特定日期的某一天..可以有人告訴我這可能是怎麼回事做..確定一年是否閏年,如果黑莓的日期是有效的

回答

5
/* 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. 

閱讀Calendar FAQ瞭解更多信息。

+0

有沒有一種使用日曆類(黑莓)的簡單方法,以及我們如何檢查輸入的日期是否有效。 – Kaddy 2009-12-18 20:09:11