java——显示当前月的日历

Java类库中的GregorianCalendar类

今天学习Java核心技术卷1,作者讲述了java中设计Date类和GregorianCalendar类的大概历程,Date类的特点是特定的时间点,它的原理即距离UTC时间(1970年1月1日00:00:00)的毫秒数来显示推算当今的时间,但是这个原理恰恰限制了Date类的拓展性和灵活性,又由于世界各地有许多不同的记时方法,例如中国的农历、希伯来的阴历和火星历等等,所以Java类库的设计者在基于此考虑上创造了GregorianCalendar类,将保存时间和给时间点命名分开。所以标准Java类库分别包含了一个用来表示时间点的Date类和一个用来表示大家熟悉的日历表示法GregorianCalendar类。

题目:应用GregorianCalendar类编写代码显示当前月的日历,并在当前日用一个*标记,2021.6.1结果示例如下

当前月的日历

解题思路:要显示当前月的日历,首先要知晓当前的月份、日期、这个月一共几天、当前月份的1号是从周几开始算的,知道了头和尾,下面只需要正确的换行操作即可。

解题代码:

//author:su_mingyuan
import java.util.*;
import java.text.DateFormatSymbols;
public class CalendarTest1 {
    public static void main(String[] args) {
        //创建一个GregorianCalendar对象
        GregorianCalendar a = new GregorianCalendar();
        //获取今天是当月的第几天
        int today = a.get(Calendar.DAY_OF_MONTH);
        //获取当前的月份
        int month = a.get(Calendar.MONTH);
        //更改a对象中Calendar.DAY_OF_MONTH为当月的第一天
        a.set(Calendar.DAY_OF_MONTH, 1);
        //获取当月的第一天是周几
        int weekday = a.get(Calendar.DAY_OF_WEEK);
        //获取表示一周第一天的解释数值
        int firstDayOfWeek = a.getFirstDayOfWeek();
        //设置日历第一行要缩进的次数
        int indent = 0;
        //如果当月第一天不是一周的第一天,回退一天,缩进次数+1
        while(weekday != firstDayOfWeek) {
            indent++;
            a.add(Calendar.DAY_OF_MONTH, -1);
            weekday = a.get(Calendar.DAY_OF_WEEK);
        }
        //设置数组存放表示一周天数的每天的中文命名
        String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
        //打印第一行:一周每天的标识
        do {
            System.out.print("\t" + weekdayNames[weekday]);
            a.add(Calendar.DAY_OF_MONTH, 1);
            weekday = a.get(Calendar.DAY_OF_WEEK);
        }while(weekday != firstDayOfWeek);
        System.out.println();
        for(int i = 1; i <= indent; i++) {
            System.out.print("\t");
        }
        //更改当天为当前月的第一天
        a.set(Calendar.DAY_OF_MONTH, 1);
        //打印剩余天数直至该月的最后一天
        do {
            //获取当前月份的第几天
            int day = a.get(Calendar.DAY_OF_MONTH);
            System.out.print("\t" + day);
            if(day == today)System.out.print("*");
            //当前月份天数加1
            a.add(Calendar.DAY_OF_MONTH, 1);
            //获取当前月份第几天是周几
            weekday = a.get(Calendar.DAY_OF_WEEK);
            //如果当前月份第几天是表示一周中的第一天,换行
            if(weekday == firstDayOfWeek) System.out.println();
        }while(a.get(Calendar.MONTH) == month);
    }
}

部分方法解释:

DAY_OF_WEEK:

The day-of-week, such as Tuesday.
This represents the standard concept of the day of the week. In the default ISO calendar system, this has values from Monday (1) to Sunday (7). The DayOfWeek class can be used to interpret the result.
Most non-ISO calendar systems also define a seven day week that aligns with ISO. Those calendar systems must also use the same numbering system, from Monday (1) to Sunday (7), which allows DayOfWeek to be used.
Calendar systems that do not have a standard seven day week should implement this field if they have a similar concept of named or numbered days within a period similar to a week. It is recommended that the numbering starts from 1.

DAY_OF_MONTH:

The day-of-month.
This represents the concept of the day within the month. In the default ISO calendar system, this has values from 1 to 31 in most months. April, June, September, November have days from 1 to 30, while February has days from 1 to 28, or 29 in a leap year.
Non-ISO calendar systems should implement this field using the most recognized day-of-month values for users of the calendar system. Normally, this is a count of days from 1 to the length of the month.

getFirstDayOfWeek:

Gets the first day-of-week.
The first day-of-week varies by culture. For example, the US uses Sunday, while France and the ISO-8601 standard use Monday. This method returns the first day using the standard DayOfWeek enum.
Returns:the first day-of-week, not null

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值