• LocalDate、LocalTime、LocalDateTime常用方法


    一、Date与LocalDate、LocalTime、LocalDateTime互转

    1.Date转LocalDate、LocalTime、LocalDateTime

            Date date = new Date();
            Instant instant = date.toInstant();
            Instant now = Instant.now(); //Instant.now()与new Date().toInstant()的结果一样,时间戳
            ZoneId zoneId = ZoneId.systemDefault(); //时区,zoneId = Asia/Shanghai
            ZonedDateTime zonedDateTime = instant.atZone(zoneId);
            LocalDate localDate = zonedDateTime.toLocalDate();
            LocalTime localTime = zonedDateTime.toLocalTime();
            LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    或者

            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
            LocalDate localDate = localDateTime.toLocalDate();
            LocalTime localTime = localDateTime.toLocalTime();
    
    • 1
    • 2
    • 3

    组成工具方法:

        public LocalDateTime getLocalDateTime(Date date) {
            return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        }
    
    • 1
    • 2
    • 3

    或者

        public LocalDateTime getLocalDateTime(Date date) {
            return LocalDateTime.of(date.getYear(), date.getMonth(), date.getDay(), date.getHours(), date.getMinutes(), date.getSeconds());
        }
    
    • 1
    • 2
    • 3

    date的以上get方法已过期,不建议使用

    2.LocalDate、LocalTime、LocalDateTime转Date

            LocalDateTime now = LocalDateTime.now();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.now());
            Instant instant = now.atZone(zoneId).toInstant();
            Date date = Date.from(instant);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    二、LocalDate常用方法
    1.获取当月第一天和最后一天

            LocalDate now = LocalDate.now();
            LocalDate firstDay = now.with(TemporalAdjusters.firstDayOfMonth()); //当月第一天
            LocalDate lastDay = now.with(TemporalAdjusters.lastDayOfMonth()); //当月最后一天
    
    • 1
    • 2
    • 3

    2.日期加减
    minusXXX:减
    plusXXX:加
    在这里插入图片描述
    在这里插入图片描述
    3.日期比较
    相等:isEqual

        int compareTo0(LocalDate otherDate) {
            int cmp = (year - otherDate.year);
            if (cmp == 0) {
                cmp = (month - otherDate.month);
                if (cmp == 0) {
                    cmp = (day - otherDate.day);
                }
            }
            return cmp;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    大于:isAfter
    小于:isBefore

    4.分别获取年月日

            int year = now.getYear();
            int monthValue = now.getMonthValue();
            int dayOfMonth = now.getDayOfMonth();
    
    • 1
    • 2
    • 3

    5.两个日期相差多少
    使用Period的静态方法between

        public static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive) {
            return startDateInclusive.until(endDateExclusive);
        }
    
    • 1
    • 2
    • 3

    返回值是一个Period对象,可以用getXXX方法获取对应的数字,如getMonths
    从源码可以看出来实际调用的还是LocalDate的until方法

    LocalDate有两个until方法:
    public Period until(ChronoLocalDate endDateExclusive)
    public long until(Temporal endExclusive, TemporalUnit unit)
    以上两个方法都是参数-方法调用者,第一个方法将结果组装成Period对象,可以用getXXX方法获取对应的数字,如getMonths;第二个方法有一个参数unit,它表示将结果转换成什么,如ChronoUnit.DAYS是将结果转换成天。
    ChronoUnit是TemporalUnit 的实现类。

    6.修改日期
    withXXX:将对应的位置修改为指定的数字
    例:
    将当前年修改为2021,月修改为2,其他不变:LocalDate.now().withMonth(2).withYear(2021)

    7.当前月、年有多少天

            LocalDate now = LocalDate.now();
            int day = now.lengthOfMonth(); //本月有多少天
            int month = now.lengthOfYear(); //今年有多少天
    
    • 1
    • 2
    • 3

    源码:

        public int lengthOfMonth() {
            switch (month) {
                case 2:
                    return (isLeapYear() ? 29 : 28);
                case 4:
                case 6:
                case 9:
                case 11:
                    return 30;
                default:
                    return 31;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
        public int lengthOfYear() {
            return (isLeapYear() ? 366 : 365);
        }
    
    • 1
    • 2
    • 3
        public boolean isLeapYear(long prolepticYear) {
            return ((prolepticYear & 3) == 0) && ((prolepticYear % 100) != 0 || (prolepticYear % 400) == 0);
        }
    
    • 1
    • 2
    • 3

    三、LocalTime、LocalDateTime常用方法与以上类似

  • 相关阅读:
    【Spring Cloud】Gateway服务网关
    希尔贝壳入选“北京市人工智能大模型高质量数据集发布(第二批)”合作企业
    ROS学习总结十七:自定义消息的使用
    NPM 使用入门
    Java基础35 面向对象三大特征之继承
    探索TiDB Lightning的源码来解决发现的bug
    实习面试记录
    LabVIEW样式检查表3
    fastadmin的导出到excel功能
    Golang gorm 常用新增方法
  • 原文地址:https://blog.csdn.net/weixin_44084024/article/details/126362860