• Java生成日历(使用LocalDate)


    package com.sun.lesson02;
    
    import java.time.DayOfWeek;
    import java.time.LocalDate;
    
    public class LocalDate01 {
        public static void main(String[] args) {
            LocalDate date = LocalDate.now();
            int month = date.getMonthValue();
            int today = date.getDayOfMonth();
    
            date = date.minusDays(today - 1);
            DayOfWeek weekday = date.getDayOfWeek();
            int value = weekday.getValue();
    
            System.out.println("Mon Tue Wed Thu Fri Sat Sun");
            for (int i = 1; i < value; i++) {
                System.out.print("    ");
            }
    
            while (date.getMonthValue() == month) {
                System.out.printf("%3d", date.getDayOfMonth());
                if(date.getDayOfMonth() == today) {
                    System.out.print("*");
                }
                else {
                    System.out.print(" ");
                }
                date = date.plusDays(1);
                if(date.getDayOfWeek().getValue() == 1) System.out.println();
            }
            if(date.getDayOfWeek().getValue() != 1) System.out.println();
        }
    }

    关于minusDays,其内部源码形式如下:

       public LocalDate minusDays(long daysToSubtract) {
            return (daysToSubtract == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-daysToSubtract));
        }

    如果当天是1号,today-1的输入就是0,然后三目运算会返回1;

    如果当天不是1号,那么today就会加上-(today-1),也就是1.

  • 相关阅读:
    bzoj4010 [HNOI2015]菜肴制作
    PHP--------TP中的ajax请求
    二维数组去重
    手机号138-0013-8000格式存储
    spring4-2-bean配置-1-依赖注入
    spring4-1-Spring的简单介绍
    Result Grouping / Field Collapsing-结果分组
    vim自动补全
    vim配置-程序员【转】
    服务端程序设计和实现总结 【转】
  • 原文地址:https://www.cnblogs.com/SuperZDK/p/14019246.html
Copyright © 2020-2023  润新知