Java实现一个简单日历

package cn.zcc.esay;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;

/**
 * @author kiryul
 * @version 1.0.0
 * @ClassName test4_calendar.java
 * @createTime 2020年04月25日 20:55:00
 * @Description 日历
 */
public class test4_calendar {
    public static void main(String[] args) {
         final String[] WEEKNAME={"星期一","星期二","星期三","星期四","星期五","星期六","星期天"};
        LocalDate date=LocalDate.now();
        int yearNum=date.getYear();
        int monthNum=date.getMonthValue();
        int dayNum=date.getDayOfMonth();
        DayOfWeek week=date.getDayOfWeek();
        Month month=date.getMonth();
        //判断是否为闰年
        boolean leap=date.isLeapYear();
        //获取当月的天数
        int monthdays=month.length(leap);

        LocalDate first=date.withDayOfMonth(1);
        int head=first.getDayOfWeek().getValue();

        System.out.println("*****************************************************************");
        System.out.println("Today:"+yearNum+"-"+monthNum+"-"+dayNum+"    "+week.name());
        System.out.println("*****************************************************************");
        System.out.println(month.name());
        System.out.println("-----------------------------------------------------------------");
        for (int i = 0; i < WEEKNAME.length; i++) {
            System.out.print(WEEKNAME[i]+"|");
        }
        System.out.println();
        int i=1;
            while (i<monthdays){
                for (int j = 1; j <= 7; j++) {
                    if(head!=j) System.out.print("       ");
                    else {
                        if(i>monthdays) break;
                        if(i==dayNum) System.out.print("*");
                        if(i<10) System.out.print("   "+i+"  ");
                        else System.out.print("  "+i+"  ");
                        i++;
                        head++;
                    }
                }
                head=1;
                System.out.println();
            }

    }
}

输出:

*****************************************************************
Today:2020-4-25    SATURDAY
*****************************************************************
APRIL
-----------------------------------------------------------------
星期一|星期二|星期三|星期四|星期五|星期六|星期天|
                 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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值