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
 

 

你可能感兴趣的:(Java)