海南大学园林园艺周鹏:大家帮我个忙,JAVA程序的题目!在线等答案!

来源:百度文库 编辑:高考问答 时间:2024/05/01 08:41:36
用JAVA写一个,当输入一个年份的时候,比如输入: 2006年5月 就输出5月份的最后一个日,“31日”。输入随便哪年哪月都可以输出的哦!
不需要有界面的,DOS的界面的就可以了!
要 swich实现的!

/**
* 获取输入月份(yyyymm)的最后一天日期(dd)
*
* @param String
* @return String
* @throws Exception
*/
public String getDate(String yearmonth) throws Exception {
int year = 0;
int month = 0;
String enddate = "";
try {
year = (new Integer(yearmonth.substring(0, 4))).intValue();
month = (new Integer(yearmonth.substring(4, 6))).intValue();
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
enddate = "31";
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
enddate = "30";
} else if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) {
enddate = "29";
} else {
enddate = "28";
}
} catch (Exception ex) {
ex.printStackTrace();
}
return enddate;
}

public String getDate(String yearmonth) throws Exception {
int year = 0;
int month = 0;
String enddate = "";
try {
year = (new Integer(yearmonth.substring(0, 4))).intValue();
month = (new Integer(yearmonth.substring(4, 6))).intValue();
switch month
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
enddate = "31";
break;
case 4:
case 6:
case 9:
case 11:
enddate = "30";
break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
enddate = "29";
}
else
{
enddate = "28";
}
break;
} catch (Exception ex) {
ex.printStackTrace();
}
return enddate;
}

楼上写得不错