public class YearDateTest {
public static void main(String[] args) {
System.out.println("Please input year:");
Scanner sc=new Scanner(System.in);
int year=sc.nextInt();
sc.close();
String[] month= {"January","Febuary","March","April","May","June","July","August","September","October","November","December"};
Calendar cal=Calendar.getInstance();
//设置日期为指定年份对应日期
cal.set(Calendar.YEAR,year);
//获取1月1日对应的星期值
int firstWeekday=cal.getFirstDayOfWeek();
//获取本地化的标准星期名称
String[] weekdaysNames=new DateFormatSymbols().getShortWeekdays();
for(int i=0;i<12;i++) {
//第一月到第十二月 注意月份值减1
cal.set(Calendar.MONTH,i);
//从第一天开始
cal.set(Calendar.DAY_OF_MONTH,1);
int Month=cal.get(Calendar.MONTH);
int weekday=cal.get(Calendar.DAY_OF_WEEK);
System.out.println("Month's name is "+month[i]);
System.out.println("---------------------------");
int count=0;
//确定第一天在日历中的位置
while(weekday!=firstWeekday) {
count++;
cal.add(Calendar.DAY_OF_MONTH,-1);
weekday=cal.get(Calendar.DAY_OF_WEEK);
}
//标准化星期值
do {
System.out.printf("%4s",weekdaysNames[weekday]);
cal.add(Calendar.DAY_OF_WEEK,1);
weekday=cal.get(Calendar.DAY_OF_WEEK);
}while(weekday!=firstWeekday);
System.out.println();
for(int j=1;j<=count;j++) {
System.out.printf(" ");
}
//重置为当月第一天
cal.set(Calendar.DAY_OF_MONTH,1);
//循环输出本月中的每一天 直至跳出当月
do {
System.out.printf("%4d ",cal.get(Calendar.DAY_OF_MONTH));
cal.add(Calendar.DAY_OF_MONTH,1);
weekday=cal.get(Calendar.DAY_OF_WEEK);
if(weekday==firstWeekday)
System.out.println();
}while(cal.get(Calendar.MONTH)==Month);
if(weekday!=firstWeekday)
System.out.println();
}
}
}