博客
关于我
万年历(java)
阅读量:499 次
发布时间:2019-03-07

本文共 2994 字,大约阅读时间需要 9 分钟。

计算某一天是星期几

为了计算某一天是星期几,我们可以按照以下步骤编写代码:

import java.util.Scanner;public class Demo1 {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        System.out.println("请输入年份");        int year = sc.nextInt();        System.out.println("请输入月份");        int month = sc.nextInt();        System.out.println("请输入天数");        int day = sc.nextInt();        int totalDay = 0;        // 计算从1900年1月1日到用户输入日期的总天数        for (int i = 1900; i < year; i++) {            totalDay += 365;        }        // 调整闰年所增加的天数        for (int i = 1900; i < year; i++) {            if (is闰年(i)) {                totalDay++;            }        }        // 计算当前年份的月份天数总和(不含当前月份的天数),然后加上当前日期        for (int month = 1; month <= inputMonth; month++) {            totalDay += daysInMonth(month);        }        totalDay += day - 1;        // 确定星期几的计算方式        int h = (int) (totalDay + 5) % 7;        if (h <= 0) h += 7;        int dayOfWeek = h;        // 输出结果        System.out.println("星期" + dayOfWeek);    }    private static boolean is闰年(int year) {        if (year % 4 != 0) return false;        if (year % 100 != 0) return true;        if (year % 400 == 0) return true;        return false;    }    private static int daysInMonth(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;            default:                return 31;        }    }}

这个代码首先读取用户输入的年、月、日,然后计算从1900年1月1日到目标日期之间的总天数。接着根据闰年的判断来调整天数,最后确定目标日期是星期几。

打印月份

以下代码用于打印某一年的某一月份的日期分布:

import java.util.Scanner;public class Demo2 {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        System.out.println("请输入年份");        int year = sc.nextInt();        System.out.println("请输入月份");        int month = sc.nextInt();        // 确定当前年份的月份名称        System.out.println(monthName(month));        // 打印月份日期分布        System.out.println("日   月   星期日   星期一   星期二   星期三   星期四   星期五   星期六");        for (int day = 1; day <= daysInMonth(month); day++) {            System.out.printf("%2d", day);        }    }    private static String monthName(int month) {        switch (month) {            case 1: return "一月";            case 2: return "二月";            case 3: return "三月";            case 4: return "四月";            case 5: return "五月";            case 6: return "六月";            case 7: return "七月";            case 8: return "八月";            case 9: return "九月";            case 10: return "十月";            case 11: return "十一月";            case 12: return "十二月";            default: return "";        }    }    private static int daysInMonth(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;            default:                return 0;        }    }}

这个代码读取年份和月份后,根据月份名称和日期分布来输出所需的内容。

转载地址:http://hlhcz.baihongyu.com/

你可能感兴趣的文章
nova基于ubs机制扩展scheduler-filter
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>
NPM 2FA双重认证的设置方法
查看>>
npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
查看>>
npm build报错Cannot find module ‘webpack‘解决方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm ERR! fatal: unable to connect to github.com:
查看>>
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 Failed to connect to github.com port 443 的解决方法
查看>>