Java打印年月日历

7 篇文章 1 订阅

一.功能

根据用户输入的年份和月份,在控制台打印出对应月份的日历。

二.算法讲解

要打印一个月的日历只需要知道两件事。

1.这个月第一天是星期几

本例子中,使用蔡勒公式,判断某年某月的一号是周几,关于蔡勒公式不了解的同学可以百度一下,这里不作讲解。
使用"蔡勒公式"计算某月第一天是周几的好处有:
​(1).不用选择参考年月日;
(2)不用计算参考日到计算日的间隔天数;
(3)不用计算一年有多少天。

2.这个月有多少天

使用switch-case语句进行判断,大月31天,小月30天,2月闰年29天,平年28天。

三.源码

import java.util.Scanner;

public class DateUtils {

    /* 判断是否为闰年 */
    public boolean isleap(int year) {
        return (year % 4 == 0) && (year % 100 != 0) || year % 400 == 0;
    }

    /* 判断这个月有多少天 */
    public int daysOfmonth(int year, int month) {
        switch (month) {
            /* 大月 */
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            /* 小月 */
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            /* 二月 */
            case 2:
                if (isleap(year))
                    return 29;
                else
                    return 28;
            default:
                System.out.println("程序错误:输入的月份有误!");
                return 0;
        }
    }

    /**
     * 蔡勒公式,判断某年某月的一号是周几
     * 
     * @param year  年
     * @param month 月
     * @return week(0,6)
     */
    private int weekOffirst(int year, int month) {
        int m = month;
        int d = 1;
        if (month <= 2) { /* 对小于2的月份进行修正 */
            year--;
            m = month + 12;
        }
        int y = year % 100;
        int c = year / 100;// 世纪数减1
        int w = (y + y / 4 + c / 4 - 2 * c + (13 * (m + 1) / 5) + d - 1) % 7;
        if (w < 0) /* 修正计算结果是负数的情况 */
            w += 7;
        return w;
    }

    /* 打印日历 */
    public void dataprint(int year, int month) {
        System.out.println("\t\t" + year + "年" + " " + month + "月");
        System.out.println("日\t一\t二\t三\t四\t五\t六");
        int week = weekOffirst(year, month);
        int[][] data = new int[6][7];
        int daynum = 1;
        for (int i = 0; daynum <= daysOfmonth(year, month); i++) {
            for (int j = 0; j < 7 && daynum <= daysOfmonth(year, month); j++) {
                if (i == 0 && j < week) {
                    data[i][j] = 0;
                    System.out.print("\t");
                } else {
                    data[i][j] = daynum++;
                    System.out.print(data[i][j] + "\t");
                }
            }
            System.out.println();
        }
    }

    /* 主函数入口 */
    public static void main(String[] args) {
        DateUtils du = new DateUtils();
        Scanner scn = new Scanner(System.in);
        System.out.print("输入要打印的年:");
        int year = scn.nextInt();
        System.out.print("输入要打印的月:");
        int month = scn.nextInt();
        scn.close();
        du.dataprint(year, month);
    }
}

效果预览

2020年1月

  • 10
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用 Java 中的 `Calendar` 类来获取指定年月的日历。以下是一个示例代码: ```java import java.util.Calendar; public class CalendarExample { public static void main(String[] args) { int year = 2022; int month = 1; // January (0 - 11) // Create a Calendar instance Calendar calendar = Calendar.getInstance(); // Set the year and month calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month); // Get the number of days in the month int numDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); // Print the calendar System.out.println("Sun Mon Tue Wed Thu Fri Sat"); // Set the calendar to the first day of the month calendar.set(Calendar.DAY_OF_MONTH, 1); // Print the first week of the month int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; for (int i = 0; i < dayOfWeek; i++) { System.out.print(" "); } for (int i = 1; i <= 7 - dayOfWeek; i++) { System.out.printf("%3d ", i); } System.out.println(); // Print the remaining weeks of the month for (int i = 7 - dayOfWeek + 1; i <= numDays; i++) { System.out.printf("%3d ", i); if ((i + dayOfWeek - 1) % 7 == 0) { System.out.println(); } } } } ``` 在此示例中,我们使用 `Calendar.getInstance()` 方法创建了一个 `Calendar` 实例。然后,我们设置了年份和月份,并使用 `getActualMaximum()` 方法获取了该月的天数。接下来,我们打印日历的表头,然后将日历设置为该月的第一天,并打印了第一周。最后,我们打印了剩余的周。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Nonoas

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

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

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

打赏作者

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

抵扣说明:

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

余额充值