【Java】制作一个万年历,使用Calendar类输出某年的日历(如2008年),并统计日期的个位数与相应的星期恰好相同的总天数

总结了一下很多人对这题的思路解法,归纳出了自己的解法,觉得能让大家更好理解掌握,希望对大家有所帮助!

package ex06;
import java.util.Calendar;
/**
 * 制作一个万年历
 * @content:使用Calendar类输出某年(如2008年)的日历,并统计日期的个位数与相应的星期恰好相同的总天数
 * @author:小何学长
 */
public class P160_4_CalendarDemo {
	//找到与相应的星期恰好相同的目标
	static String[] weekday = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
	//设置相应的月份天数
	static int[] Monthday = { 0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	//设置打印的年份(也可以在主函数中使用Scanner录入,但就不能用final定义)
	static final int year = 2008;//高级版这里可以定位自定义输入,即可求任意一年的日历
	public static void main(String[] args) {
		System.out.println("使用Calendar类打印2008年的日历如下:");
		//判断是否为闰年,决定2月份的天数
		if (Isleapyear())
			Monthday[2] = 29;
		else
			Monthday[2] = 28;		
		int date = 0,count = 0,days = 0,weekdate;
		//循环打印月份信息
		for (int month = 1; month <= 12; month++) {
			printCalender(month);
			System.out.println();
			//固定的公式求日期对应的星期是星期几
			for (int day = 1; day <= Monthday[month]; day++) {
				days++;
				weekdate = ((year - 1) + (int) (year - 1) / 4 - (int) (year - 1) / 100 + (int) (year - 1) / 400 + days) % 7;
				date = day % 10;
				if (date == weekdate) {
					System.out.println(year + "年" + month + "月" + day + "日恰好是" + weekday[weekdate]);
					count++;
				}
			}
			System.out.println("=====================================================");
			
		}
		System.out.println(year + "年日期的个位数与相应的星期恰好相同的总天数为:" + count);
	}
	
	public static void printCalender(int month) {
		System.out.println("\t\t\t" + month + "月\n");
		//Calendar是抽象类,通过方法getInstance()获取功能
		Calendar calendar = Calendar.getInstance();
		//设置日历的信息,但注意月份是从0开始的(西方文化)
		calendar.set(year, month - 1, 1);
		// 求本周开始的第一天是星期几
		int startDay = calendar.get(Calendar.DAY_OF_WEEK); 
		int count = startDay - 1;
		int maxDay = Monthday[month];
		System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
		//填补每个月开头前的空缺
		fillSpace(startDay);
		//打印日历信息
		for (int day = 1; day <= maxDay; day++) {
			System.out.print(day + "\t");
			count++;
			// 每输出7天换一次行
			if (count >= 7) { 
				count = 0;
				System.out.println();
			}
		}
	}

	public static void fillSpace(int startDay) {
		for (int i = 1; i < startDay; i++) {
			System.out.print("\t");
		}
	}

	public static boolean Isleapyear() {
		return (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
	}
}

运行结果如下:
在这里插入图片描述
中间省略其他月份,直接到结尾:
在这里插入图片描述

如果你想打印某年某月具体的日历,只要把月份的for循环去掉,自己录入相应的月份数组打印就好了,如果还想加入查询当前时间等,就加上代码system.currentTime()就可以了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

满幕星何

你的鼓励是我创作的最佳动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值