计算天数

/**
* 输入某年某月某日,判断这一天是这一年的第几天
*
*
*/
public class ComputeDays {

public static void main(String[] args) {
int year = 2004;
int month = 10;
int day = 5;

int result = 0;

for (int i = 1; i < month; i++) {//计算在该月份之前经过的天数
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:// 月份为大月,天数加31
result += 31;
break;
case 4:
case 6:
case 9:
case 11:// 月数为小月,天数加30
result += 30;
break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {// 闰年,天数加29
result += 29;
} else {// 非闰年,天数加28
result += 28;

}
break;
}
}

System.out.println(year + "年" + month + "月" + day + "日,是该年的第" + result
+ "天");
}

}

你可能感兴趣的:(计算)