zoukankan      html  css  js  c++  java
  • java8 Date LocalDate LocaDateTime 互相转化

    java 8中 java.util.Date 类新增了两个方法,分别是from(Instant instant)和toInstant()方法

    // Obtains an instance of Date from an Instant object.
    public static Date from(Instant instant) {
        try {
            return new Date(instant.toEpochMilli());
        } catch (ArithmeticException ex) {
            throw new IllegalArgumentException(ex);
        }
    }
    
    // Converts this Date object to an Instant.
    public Instant toInstant() {
        return Instant.ofEpochMilli(getTime());
    }

    这两个方法使我们可以方便的实现将旧的日期类转换为新的日期类,具体思路都是通过Instant当中介,然后通过Instant来创建LocalDateTime(这个类可以很容易获取LocalDate和LocalTime),新的日期类转旧的也是如此,将新的先转成LocalDateTime,然后获取Instant,接着转成Date,具体实现细节如下:

        @Test
        public void method3(){
            LocalDateTime localDateTime  = LocalDateTime.now();
            System.out.println("localDateTime: "+localDateTime);
            Instant instant =  localDateTime.atZone(ZoneId.systemDefault()).toInstant();
            Date date = Date.from(instant);
            System.out.println("date: "+date);
        }
    
    输出结果
    localDateTime: 2018-11-19T10:22:00.712
    date: Mon Nov 19 10:22:00 CST 2018
    LocalDateTime---------->Date
        @Test
        public void method4(){
            LocalDate localDate = LocalDate.now();
            System.out.println("localDate: "+localDate);
            Instant instant =  localDate.atStartOfDay(ZoneId.systemDefault()).toInstant();
            Date date = Date.from(instant);
            System.out.println("date: "+date);
        }
    
    输出结果
    localDate: 2018-11-19
    date: Mon Nov 19 00:00:00 CST 2018
    LocalDate------->Date
        @Test
        public void method5(){
            LocalDateTime localDateTime = LocalDateTime.now();
            LocalDate localDate = localDateTime.toLocalDate();
            System.out.println(localDate);
        }
        
        @Test
        public void method6(){
            LocalDate localDate = LocalDate.now();
            LocalDateTime localDateTime = localDate.atStartOfDay();
            System.out.println(localDateTime);
        }
    LocalDate LocalDateTime
        @Test
        public void method1(){
            Date d1 = new Date();
            System.out.println("date: "+d1);
            Instant instant =  d1.toInstant();
            LocalDateTime local1 = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            System.out.println("LocalDateTime: "+local1);
        }
    
    
    
    输出结果
    date: Mon Nov 19 09:37:14 CST 2018
    LocalDateTime: 2018-11-19T09:37:14.063
    Date ----->LocalDateTime
        @Test
        public void method2(){
            Date d1 = new Date();
            System.out.println("date: "+d1);
            Instant instant =  d1.toInstant();
            LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            LocalDate ld = ldt.toLocalDate();
            System.out.println("LocalDate: "+ld);
        }
    
    输出结果
    date: Mon Nov 19 09:46:30 CST 2018
    LocalDate: 2018-11-19
    Date ------>LocalDate
  • 相关阅读:
    【邀请函】小投入 大产出—微软智能云(Azure)之CDN 专题
    Azure镜像市场再下一城,中标软件入驻开启Azure国产操作系统时代
    15分钟完成基于Azure公有云搭建远程测试环境
    独家秘笈!教你解锁移动应用新技能
    “剁手节”来了,红包你抢到了吗?
    Azure 11 月新公布
    面对故宫万千珍宝,升哲科技如何做到“朕知道了”
    高斯-克吕格投影
    cad定制快捷键
    matlab之scatter3()与plot3()函数
  • 原文地址:https://www.cnblogs.com/wenbuzhu/p/9981705.html
Copyright © 2011-2022 走看看