LocalDateTime和Date互相转换

、、、

    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime localDateTime = LocalDateTime.parse("2018-12-12 20:20:21", dateTimeFormatter);//字符转localDateTime
    String format = dateTimeFormatter.format(localDateTime);//localDateTime转字符
    System.out.println(format);
    ZoneId zone = ZoneId.systemDefault();
    Instant instant = localDateTime.atZone(zone).toInstant();
    java.util.Date date = Date.from(instant);//localDateTime转date
    System.out.println(date);
    Instant instant2 = date.toInstant();
    LocalDateTime localDateTime2 = LocalDateTime.ofInstant(instant2, zone);//date转localDateTime
    System.out.println(localDateTime2);
复制代码

、、、

转载于:https://juejin.im/post/5cd941d951882569584435aa

你可能感兴趣的:(LocalDateTime和Date互相转换)