确定一年是否跳跃以及黑莓中的日期是否有效

时间:2009-12-18 00:56:01

标签: java date blackberry java-me calendar

我从用户那里得到一个日期输入..在这种情况下,我需要确定用户输入的年份是否是闰年...并且在用户输入整个日期之后,即月份日期和年份...如果需要确定该特定日期的日期,我需要检查日期是否实际有效..有人可以告诉我如何做到这一点..

1 个答案:

答案 0 :(得分: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了解更多信息。