打印一年中的月歷
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Calendar;
public class NianLi {
public static void main(String[]args) throws IOException{
//緩沖輸入流
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("請輸入日期,如yyyy");
String rq=br.readLine();
int d=Integer.parseInt(rq);
for(int m=0;m<=11;m++){
Calendar c=Calendar.getInstance();
c.set(Calendar.YEAR, d);
c.set(Calendar.MONTH,m);
//當前日期中當前月對應的最大天數
int allDay=c.getActualMaximum(Calendar.DAY_OF_MONTH);
//當前日期中當前月第一天對應的星期數
c.set(Calendar.DAY_OF_MONTH, 1);
int firstDay = c.get(Calendar.DAY_OF_WEEK);
System.out.println("\t\t第"+(m+1)+"月的月歷");
System.out.println("日\t一\t二\t三\t四\t五\t六");
c.set(Calendar.MONTH,m);
for (int j = 1; j < firstDay; j++) {
System.out.print("\t");
}
for (int i = 1; i <= allDay; i++) {
System.out.print(i + "\t");
//每到星期六就換行
c.set(Calendar.DATE, i);
if (c.get(Calendar.DAY_OF_WEEK) % 7 == 0) {
System.out.println("\n");
}
}
System.out.println("\n");
}
}
}
打印一年中的月歷