1. public static void main(String[] args) {
    2. //日期格式化
    3. LocalDateTime dateTime = LocalDateTime.now();
    4. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
    5. String time = dateTime.format(dtf);
    6. System.out.println(time);
    7. System.out.println("------------------Date==》LocalDateTime------------------");
    8. //获取当前时间
    9. Date date = new Date();
    10. //转成时间戳
    11. Instant instant = date.toInstant();
    12. //时间戳 + 时区
    13. //使用LocalDateTime 中 API
    14. LocalDateTime dateTime1 = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    15. System.out.println(dateTime1);
    16. System.out.println("------------------LocalDateTime==》Date------------------");
    17. LocalDateTime dateTime2 = LocalDateTime.now();
    18. //获得LocalDateTime时间戳
    19. Instant instant1 = dateTime2.atZone(ZoneId.systemDefault()).toInstant();
    20. //使用Date中API
    21. Date from = Date.from(instant1);
    22. System.out.println(from);
    23. }