如果要月历,只要把月份循环那里修改链穗御下,直接调用月历方法既可import java.text.DateFormatSymbols;import java.util.Calendar;import javax.swing.JOptionPane;public class YearCalendar { public static void main(String[] args) { final String title = getCalTitle(); String input = JOptionPane.showInputDialog("Please input year"); try{ if(!input.trim().matches("^\\d{4}$")){ throw new NumberFormatException(); } int year = Integer.parseInt(input.trim()); System.out.println("------- Calendar For Year " + year + " ----------------"); String[] monthTitles = new DateFormatSymbols().getMonths(); for(int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month++){ System.out.println("\t********** "族祥 + monthTitles[month] + " *********"棚岩); System.out.println(title); generateMonthlyCalendar(year, month); System.out.println("\n\n"); }}catch(NumberFormatException nbFmtExp){ JOptionPane.showMessageDialog(null, "Error data foramt! Date should be 4 digits only format yyyy"); System.exit(0); }} private static String getCalTitle() { StringBuffer sb = new StringBuffer(); String[] ary = new DateFormatSymbols().getShortWeekdays(); for(int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++){ sb.append(ary[i]+ "\t"); } return sb.toString(); } private static void generateMonthlyCalendar(int year, int month) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month); cal.set(Calendar.DATE, 1); int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); int i = 0; for(i = Calendar.SUNDAY; i < cal.get(Calendar.DAY_OF_WEEK); i++){ System.out.print(" \t"); } while(cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY){ System.out.print(cal.get(Calendar.DATE) + "\t"); cal.add(Calendar.DATE, 1); } int weekDay = Calendar.SATURDAY; int day = cal.get(Calendar.DATE); while(day <= maxDay){ if(weekDay == Calendar.SATURDAY){ System.out.println(); weekDay = Calendar.SUNDAY; }else{ weekDay++; } System.out.print(day++ + "\t");} }}--------------------------------JDK 1.5结果------- Calendar For Year 2011 ---------------- ********** January *********Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ********** February *********Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ********** March *********Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ********** April *********Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ********** May *********Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ********** June *********Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ********** July *********Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ********** August *********Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ********** September *********Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ********** October *********Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ********** November *********Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ********** December *********Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
相关文章
-
java和c语言学那个更容易一点?
2023-06-13 12:48 阅读(589) -
java 多个线程从一张表内取数据,且不重复
2023-06-12 21:08 阅读(562) -
java中从服务器返回的是native2ascii码 如何转换为UTF-8?
2023-06-11 00:44 阅读(618)
1 Java中String和Int的相互转换
652 阅读
2 java中的jersey是干什么用的
576 阅读
3 java字符串为json格式的函数
637 阅读
4 javaqq2021怎么安装
557 阅读
5 JavaScript操作用友华表cell插件,该怎么处理
606 阅读