利用LocalDate类创建一个日历程序

    LocalDate currentdate = LocalDate.of(2021, 8, 30); //set current time
        int month = currentdate.getMonthValue(); //get day of month
        int today = currentdate.getDayOfMonth(); //get day
        currentdate = currentdate.minusDays(today - 1); //today - (today - 1) = 1
        DayOfWeek weekday = currentdate.getDayOfWeek();
        //System.out.println(weekday);
        int value = weekday.getValue();  //1 is Monday ... 7 is Sunday
        
        System.out.println("Mon Tue Wed Thu Fri Sat Sun");
        for(int i = 1;i < value;i ++)    System.out.print("    ");
        
        while(currentdate.getMonthValue() == month)
        {
            System.out.printf("%3d", currentdate.getDayOfMonth()); //
            if(currentdate.getDayOfMonth() == today)    System.out.print("*");
            else System.out.print(" ");
            currentdate = currentdate.plusDays(1);
            if(currentdate.getDayOfWeek().getValue() == 1)    System.out.println();
        }
        if(currentdate.getDayOfWeek().getValue() != 1)    System.out.println();
    }

利用LocalDate类编写一个日历程序,能处理星期几以及各月天数不同的复杂问题。(本代码来自《Java核心技术 卷I》一书)

posted @ 2021-08-14 16:57  Dre_am_tale  阅读(125)  评论(0编辑  收藏  举报