根据输入年份输出当年的日历表

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();
		}
	}
}

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值