<span style="font-size:18px;">package scanner类;
import java.util.Calendar;
public class CalendarBean {
int year=0,month=0;
public void setYear(int year){
this.year=year;
}
public void setMonth(int month){
this.month=month;
}
public String [] getCalendar(){
String []a=new String[42];
Calendar rill=Calendar.getInstance();
rill.set(year,month-1,1);
int weekDay=rill.get(Calendar.DAY_OF_WEEK)-1;//計算出一號的星期
int day=0;
if(month==1||month==3||month==5||month==7||month==8||month==10
||month==12)
day=31;
if(month==4||month==6||month==9||month==11)
day=30;
if(month==2){
if(((year%4==0)&&(year%100!=0))||(year%400==0))
day=29;
else
day=28;
}
for(int i=0;i<weekDay;i++)
a[i]=" ";
for(int i=weekDay,n=1;i<weekDay+day;i++){
a[i]=String.valueOf(n) ;
n++;
}
for(int i=weekDay+day;i<a.length;i++)
a[i]=" ";
return a;
}
}
</span>
<span style="font-size:18px;">package scanner类;
public class example03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
CalendarBean cb = new CalendarBean();
cb.setYear(2016);
cb.setMonth(7);
String [] a=cb.getCalendar();//返回號碼的一維數組
char[] str =" 日 一 二 三 四 五 六".toCharArray();
for(char c:str){
System.out.printf("%3c",c);
}
for(int i=0;i<a.length;i++){
if(i%7==0)
System.out.println("");
System.out.printf("%4s",a[i]);
}
}
}
</span>