栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java课后题第十三章:13.4(显示日历)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java课后题第十三章:13.4(显示日历)

目录

1、当月天数

2、月初第一天为周几

3、Display Calendar

4、Sample Print

1、当月天数
        Calendar a = new GregorianCalendar(year, month - 1, 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1); //为什么是最后一天???
        int m = a.get(Calendar.DATE);

2、月初第一天为周几
        a.roll(Calendar.DATE, 1);
        int n = a.get(Calendar.DAY_OF_WEEK);

3、Display Calendar
public class DisplayCalendar {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("The year:");
        int year = input.nextInt();
        System.out.print("The month:");
        int month = input.nextInt();

        Calendar a = new GregorianCalendar(year, month - 1, 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1); //为什么是最后一天???
        int m = a.get(Calendar.DATE);
        a.roll(Calendar.DATE, 1);
        int n = a.get(Calendar.DAY_OF_WEEK);

        String[] dayNameOfWeek = {"Sun", "Mon", "Tue",
                "Wed", "Thu", "Fri", "Sat"};
        for (int i = 0; i < 7; i++) {
            System.out.print(dayNameOfWeek[i] + "t");
        }
        System.out.println("n" + "___________________________");

        int i = 1;
        while (i < n) {
            System.out.print("t");
            i++;
        }
        for (i = 1; i <= m; i++) {
            if ((n - 1 + i) % 7 == 0)
                System.out.println(i + "t");
            else
                System.out.print(i + "t");
        }
    }
}

4、Sample Print
The year:2021
The month:11
Sun	Mon	Tue	Wed	Thu	Fri	Sat	
___________________________
	1	2	3	4	5	6	
7	8	9	10	11	12	13	
14	15	16	17	18	19	20	
21	22	23	24	25	26	27	
28	29	30	
Process finished with exit code 0

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/591663.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号