计算闰年

xda*_*mon 0 java

我在Eclipse中使用Java编写了这个程序.
我能够利用我发现的公式,我在注释部分解释过.
使用for循环我可以遍历一年中的每个月,我在该代码中感觉很好,对我来说似乎干净顺利.也许我可以给变量全名以使一切更具可读性但我只是在其基本本质中使用公式:)

好吧,我的问题是它没有像2008年那样正确计算......闰年.
我知道如果(年%400 == 0 ||(年%4 == 0 &&年%100!= 0))那么我们有一个闰年.

也许如果这一年是闰年,我需要从某个月减去一定的天数.

任何解决方案,或某些方向将是非常感谢:)

package exercises;

public class E28 {
/*
 * Display the first days of each month
 * Enter the year
 * Enter first day of the year
 * 
 * h = (q + (26 * (m + 1)) / 10 + k + k/4 + j/4 + 5j) % 7
 * 
 * h is the day of the week (0: Saturday, 1: Sunday ......)
 * q is the day of the month
 * m is the month (3: March 4: April.... January and Feburary are 13 and 14)
 * j is the century (year / 100)
 * k is the year of the century (year %100)
 * 
 */
public static void main(String[] args) {
    java.util.Scanner input = new java.util.Scanner(System.in);
    System.out.print("Enter the year: ");
    int year = input.nextInt();

    int j = year / 100; // Find century for formula
    int k = year % 100; // Find year of century for formula

    // Loop iterates 12 times. Guess why.
    for (int i = 1, m = i; i <= 12; i++) { // Make m = i. So loop processes formula once for each month
        if (m == 1 || m == 2)              
            m += 12;                       // Formula requires that Jan and Feb are represented as 13 and 14
        else
            m = i;                         // if not jan or feb, then set m to i

        int h = (1 + (26 * (m + 1)) / 10 + k + k/4 + j/4 + 5 * j) % 7; // Formula created by a really smart man somewhere
        // I let the control variable i steer the direction of the formual's m value

        String day;
        if (h == 0)
            day = "Saturday";
        else if (h == 1)
            day = "Sunday";
        else if (h == 2)
            day = "Monday";
        else if (h == 3)
            day = "Tuesday";
        else if (h == 4)
            day = "Wednesday";
        else if (h == 5)
            day = "Thursday";
        else 
            day = "Friday";

        switch (m) {
        case 13: 
            System.out.println("January 1, " + year + " is " + day);
            break;
        case 14:
            System.out.println("Feburary 1, " + year + " is " + day);
            break;
        case 3:
            System.out.println("March 1, " + year + " is " + day);
            break;
        case 4:
            System.out.println("April 1, " + year + " is " + day);
            break;
        case 5:
            System.out.println("May 1, " + year + " is " + day);
            break;
        case 6:
            System.out.println("June 1, " + year + " is " + day);
            break;
        case 7:
            System.out.println("July 1, " + year + " is " + day);
            break;
        case 8:
            System.out.println("August 1, " + year + " is " + day);
            break;
        case 9:
            System.out.println("September 1, " + year + " is " + day);
            break;
        case 10:
            System.out.println("October 1, " + year + " is " + day);
            break;
        case 11:
            System.out.println("November 1, " + year + " is " + day);
            break;
        case 12:
            System.out.println("December 1, " + year + " is " + day);
            break;
        }
    }
}
}
Run Code Online (Sandbox Code Playgroud)

Den*_*aev 7

你需要开发自己的算法吗?GregorianCalendar类有方法:

boolean isLeapYear(int year)

如果year是闰年,则此方法返回true,否则返回false.


Dec*_*eco 5

您应该使用JodaTimeJava中的日期来处理所有事情并且它易于使用.

使用JodaTime,你不必担心闰年或任何事情,因为它会照顾你.一个简单的for循环和一个DateTime对象将为您提供所需的东西.

    int year = 2012;

    DateTime dt = new DateTime(year, 01, 01, 00, 00, 00, 00);
    for (int i = 0; i < 12; i++) {
        System.out.println(dt.toString() + " " + dt.property(DateTimeFieldType.dayOfWeek()).getAsText());
        dt = dt.plusMonths(1);
    }
Run Code Online (Sandbox Code Playgroud)

您显然会year用输入替换变量,但基本上代码的工作原理如下:

  • 从1月1日开始为年份输入初始化DateTime对象.
  • 以文本格式打印当前日期时间和星期几
  • 将DateTime对象中的月份增加1.

输出:

2012-01-01T00:00:00.000+11:00 Sunday
2012-02-01T00:00:00.000+11:00 Wednesday
2012-03-01T00:00:00.000+11:00 Thursday
. . . 
Run Code Online (Sandbox Code Playgroud)