package crk; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JOptionPane; public class Rili { /** * @param args */ public static boolean Leap(int year) { if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { return true; } else { return false; } } /** * 当月天数 * @param year 年份 * @param month 月份 * @return 当月的天数 */ public static int Month(int year, int month) { if (month==1||month==3||month==5||month==7||month==8||month==10||month==12) return 31; if (month==4||month==6||month==9||month==11) return 30; if (month==2) return Leap(year)?28:29; return 0; } /** * 当月第一天是星期几 * @param year 年份 * @param month 月份 * @return */ public static int Week(int year, int month) { Date date = new Date(); date.setYear(year-1900) ; date.setMonth(month-1) ; date.setDate(0) ; int week = date.getDay()+1; return week; } /** * 输出日历表 * @param year 年份 * @param month 月份 */ public static void Print(int year, int month) { System.out.println(" " + year + "年" + month + "月日历 /n"); System.out.println("----------------------------------------------------"); System.out.println("星期日/t星期一/t星期二/t星期三/t星期四/t星期五/t星期六/n"); int days = Month(year, month); int firstD = Week(year, month); for (int kongge = 0; kongge < firstD&& firstD!= 7; kongge++) { System.out.print("/t"); } for (int i = 1; i <=days; i++) { System.out.print(i + "/t"); if ((i + firstD) % 7 == 0) { System.out.println("/n"); } } } public static void main(String[] args) { // TODO Auto-generated method stub String yearString=JOptionPane.showInputDialog("input the year :"); int year =Integer.parseInt(yearString); String monthString=JOptionPane.showInputDialog("input the month :"); int month =Integer.parseInt(monthString); Print(year, month); } }