Java入门案例:万年历

这篇博客介绍了一个Java入门级别的实例——万年历的制作。文章通过代码展示了如何利用日期相关类和方法来完成这个任务。在代码中使用了while循环,以便进行多次测试。重点在于理解和运用Java中的日期处理技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package PerpetualCalendar;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/*
万年历项目:
分析:
    1.输入年份判断是否闰年(键盘录入年月)
    2.输入月份,判断每个月是多少天,闰年和非闰年的2月的天数
    3.设计一个方法,获取某一天是星期几
    4.获取某个月的一日是星期几,设计对应的日期位置
    5.判断:是一周的第一天就换行(这里的第一天是周一)
 */
public class PerpetualCalendar {
    public static void main(String[] args) {
        System.out.println("**************欢迎使用万年历(1~3000)**************");
        Scanner sc = new Scanner(System.in);
        int days = 0;
        int year;
        int month;
        //标记:用来记录是否是闰年
        boolean b = true;
        while (true) {
            while (true) {
                System.out.print("年(year):");
                year = sc.nextInt();
                //判断是否输入了合法年份
                if (year > 3000 || year <= 0) {
                    System.out.println("您的输入不正确");
                } else {
                    //判断是否闰年
                    if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
                        System.out.println(year + "年是闰年");
                        b = true;
                        break;
                    } else {
                        System.out.println(year + "年是平年");
                        b = false;
                        break;
                    }
                }
            }
            //while循环:判断并修改某个月的天数
            M:while (true) {
                System.out.print("月(month):");
                month = sc.nextInt();
                switch (month) {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        days = 31;
                        break M;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        days = 30;
                        break M;
                    case 2:
                        if (b) {
                            days = 29;
                        } else {
                            days = 28;
                        }
                        break M;
                    default:
                        System.out.println("您的输入不正确");
                }
            }
            System.out.println(year + "年" + month + "月有" + days + "天");

            System.out.println("周一" + "\t" + "周二" + "\t" + "周三" + "\t" + "周四" + "\t" + "周五" + "\t" + "周六" + "\t" + "周日");
            System.out.println("Monday" + "\t" + "Tuse" + "\t" + "Wednes" + "\t" + "Thurs" + "\t" + "Friday" + "\t" + "Satur" + "\t" + "Sun");
            //获取某个月的1日是星期几
            String week = getDayOfWeekByDate(year + "-" + month + "-01");
            //判断周几,以确定某月1日开始的位置,空的地方用制表符填充
            switch(week){
                case "周一":
                    break;
                case "周二":
                    System.out.print("\t\t");
                    break;
                case "周三":
                    System.out.print("\t\t\t\t");
                    break;
                case "周四":
                    System.out.print("\t\t\t\t\t\t");
                    break;
                case "周五":
                    System.out.print("\t\t\t\t\t\t\t\t");
                    break;
                case "周六":
                    System.out.print("\t\t\t\t\t\t\t\t\t\t");
                    break;
                case "周日":
                    System.out.print("\t\t\t\t\t\t\t\t\t\t\t\t");
                    break;

            }
            //按照周一~周日的顺序排列日期
            for(int x= 1;x<=days;x++){
                //判断:如果下一天是周一,就换行
                week = getDayOfWeekByDate(year + "-" + month + "-" +x);
                if(week.equals("周一")){
                    System.out.println();
                }
                if(x<10) {
                    System.out.print("0" + x + "日" + "\t");
                }else{
                    System.out.print(x + "日" + "\t");
                }
            }
            System.out.println();
        }
    }

    //根据日期 找到对应日期是星期几
    public static String getDayOfWeekByDate(String date) {
        String dayOfweek = "-1";
        try {
            SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
            Date myDate = myFormatter.parse(date);
            SimpleDateFormat formatter = new SimpleDateFormat("E");
            String str = formatter.format(myDate);
            dayOfweek = str;

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return dayOfweek;
    }
}

 输出结果:

最后出现年(year):,是因为代码整体加了while死循环,便于一次运行,多次测试;

总结:本案例的难点在于日期相关的类和方法的操作;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

劉鎠

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值