zoukankan      html  css  js  c++  java
  • Java8 Date与LocalDate互转

    reference:https://blog.csdn.net/panchang199266/article/details/95724991

    Java8 日期时间API,新增了LocalDate、LocalDateTime、LocalTime等线程安全类:

    • LocalDate:只有日期,诸如:2019-07-13
    • LocalTime:只有时间,诸如:08:30
    • LocalDateTime:日期+时间,诸如:2019-07-13 08:30

    1.Date转换成LocalDate

        public static LocalDate date2LocalDate(Date date) {
            if(null == date) {
                return null;
            }
            return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        }

    2.LocalDate转换成Date

    
        public static Date localDate2Date(LocalDate localDate) {
            if(null == localDate) {
                return null;
            }
            ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
            return Date.from(zonedDateTime.toInstant());
        }

    2.LocalDateTime转换成Date

        public static Date localDateTime2Date(LocalDateTime localDateTime) {
            return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        }

    3.LocalDate格式化

        public static String formatDate(Date date) {
            LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
  • 相关阅读:
    String对象的属性和方法
    Math对象的属性和方法
    对象Date的方法
    HTML5和CSS3阶段,我是如何学习的?
    移动端项目开发心得
    关于元素隐藏/显示的各种方法
    啊哈算法(一)
    项目心得。
    CSS篇之DIV+CSS布局
    CSS篇之动画(2)
  • 原文地址:https://www.cnblogs.com/marxtsui/p/14417076.html
Copyright © 2011-2022 走看看