急求一个JAVA编写的日历程序!

急求一个JAVA编写的日历程序,请大虾帮忙。。。
2024-12-21 22:42:42
推荐回答(1个)
回答1:

­
­
­
package mycalendar;
import java.util.*;
class ViewMonth {
int month;
int year;
ViewMonth(final int displayMonth, final int displayYear) {
month = displayMonth;
year = displayYear;
}
private String checkMonth() {
String[] months = {
"1 月" , "2 月" , "3 月",
"4 月" , "5 月" , "6 月",
"7 月" , "8 月" , "9 月",
"10 月" , "11 月" , "12 月"
};
return months[month];
}
private int checkDays() {
int[] numofDays = {
31, 28, 31,
30, 31, 30,
31, 31, 30,
31, 30, 31
};
return numofDays[month];
}
/**
* 使用此方法打印该月的日历.
*/
void printMonth() {
/* 将该月份起始处的天数留空. */
int initialSpaces = 0;
try {
/* 获取月份名称. */
String monthName = checkMonth();
System.out.println();
System.out.println("\t\t\t " + year + " 年 " + monthName );
System.out.println();
} catch (ArrayIndexOutOfBoundsException ae) {
System.out.println("超出范围 ..........");
System.exit(0);
}
GregorianCalendar cal = new GregorianCalendar(year, month, 1);
System.out.println("\t日\t一\t二\t三\t四\t五\t六");
initialSpaces = cal.get(Calendar.DAY_OF_WEEK) - 1;
/* 获取天数. */
int daysInMonth = checkDays();
/* 检查是否为闰年并为二月增加一天. */
if (cal.isLeapYear(cal.get(Calendar.YEAR)) && month == 1) {
++daysInMonth;
}
for (int ctr = 0; ctr < initialSpaces; ctr++) {
System.out.print("\t");
}
for (int ctr = 1; ctr <= daysInMonth; ctr++) {
/* 为单个日期添加空格. */
if (ctr <= 9) {
System.out.print(" ");
}
System.out.print("\t" + ctr);
/* 检查行的末尾. */
if ((initialSpaces + ctr) % 7 == 0) {
System.out.println();
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
class J7上机2 {
protected J7上机2() {
}
public static void main(String[] args) {
int month, year;
if (args.length == 2) {
System.out.println("显示日历");
System.out.println();
int mon = Integer.parseInt(args[0]);
month = mon - 1;
year = Integer.parseInt(args[1]);
} else {
Calendar today = Calendar.getInstance();
month = today.get(Calendar.MONTH);
year = today.get(Calendar.YEAR);
}
ViewMonth mv = new ViewMonth(month, year);
mv.printMonth();
}
}