Java8 - LocalDateTime和Date互转

Date to LocalDateTime:

        Date currDate = new Date();
        LocalDateTime ldt = currDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        System.out.println(ldt);

输出:

2020-01-19T15:41:02.048

LocalDateTime to Date:

        LocalDateTime localDateTime = LocalDateTime.now();
        Date date1 = Date.from(localDateTime.atZone(ZoneId.of("Asia/Shanghai")).toInstant());
        Date date2 = Date.from(localDateTime.toInstant(ZoneOffset.of("+8")));
        System.out.println(date1);
        System.out.println(date2);

输出:

Sun Jan 19 15:41:02 CST 2020
Sun Jan 19 15:41:02 CST 2020
 

发布了53 篇原创文章 · 获赞 21 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_26878363/article/details/104041521
今日推荐