JAVA请大神帮我看看我这代码哪里错?
运行后闰年的2月显示不出29天,还是28天,是怎么回事?这是一部分代码,不能全放,请大神帮我看看privatestaticStringtest(intyear,intmo...
运行后闰年的2月显示不出29天,还是28天,是怎么回事?这是一部分代码,不能全放,请大神帮我看看
private static String test(int year, int month) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month - 1);
String str = "日\t一\t二\t三\t四\t五\t六\r";
int day = getDay(year, month - 1);
c.set(Calendar.DAY_OF_MONTH, 1);
str += getStartWeizhi(c);
for (int i = 2; i <= day; i++) {
c.set(Calendar.DAY_OF_MONTH, i);
if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
str += i + "\r";
} else {
str += i + "\t"; }
}
return str;}
private static int getDay(int year, int month) {
int[] DAYS = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month != 2) {
return DAYS[month];
}
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return 29;
}
return 29;
}
private static String getStartWeizhi(Calendar c) {
int idx = c.get(Calendar.DAY_OF_WEEK);
StringBuffer buf = new StringBuffer();
for (int i = 0; i < idx - 1; i++) {
buf.append(" \t");
}
if (Calendar.SATURDAY == idx) {
buf.append(c.get(Calendar.DAY_OF_MONTH) + "\r");
} else {
buf.append(c.get(Calendar.DAY_OF_MONTH) + "\t");
}
return buf.toString();
} 展开
private static String test(int year, int month) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month - 1);
String str = "日\t一\t二\t三\t四\t五\t六\r";
int day = getDay(year, month - 1);
c.set(Calendar.DAY_OF_MONTH, 1);
str += getStartWeizhi(c);
for (int i = 2; i <= day; i++) {
c.set(Calendar.DAY_OF_MONTH, i);
if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
str += i + "\r";
} else {
str += i + "\t"; }
}
return str;}
private static int getDay(int year, int month) {
int[] DAYS = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month != 2) {
return DAYS[month];
}
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return 29;
}
return 29;
}
private static String getStartWeizhi(Calendar c) {
int idx = c.get(Calendar.DAY_OF_WEEK);
StringBuffer buf = new StringBuffer();
for (int i = 0; i < idx - 1; i++) {
buf.append(" \t");
}
if (Calendar.SATURDAY == idx) {
buf.append(c.get(Calendar.DAY_OF_MONTH) + "\r");
} else {
buf.append(c.get(Calendar.DAY_OF_MONTH) + "\t");
}
return buf.toString();
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询