展开全部
如果要月历,只要把月份循环那里修改下,直接调用月历方法既可
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(!32313133353236313431303231363533e4b893e5b19e31333264633466input.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
追问
布局真乱
已赞过
已踩过<
你对这个回答的评价是?
评论
收起