zoukankan      html  css  js  c++  java
  • LocalDateTime、LocalDate、Long、Date、String 相互转换

    DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    LocalDateTime localDateTime = LocalDateTime.parse("2019-07-31 00:00:00",dateTimeFormatter1);
    LocalDate localDate = LocalDate.parse("2019-07-31",dateTimeFormatter2);
    Date date = Date.from(LocalDateTime.parse("2019-07-31 00:00:00",dateTimeFormatter1).atZone(ZoneId.systemDefault()).toInstant());


    String strDateTime = "2019-07-31 00:00:00";
    String strDate = "2019-07-31";
    Long timestamp=1564502400000l;

    /** LocalDateTime 转 LocalDate */
    System.out.println("LocalDateTime 转 LocalDate: "+localDateTime.toLocalDate());
    /** LocalDateTime 转 Long */
    System.out.println("LocalDateTime 转 Long: "+localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
    /** LocalDateTime 转 Date */
    System.out.println("LocalDateTime 转 Date: "+Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()));
    /** LocalDateTime 转 String */
    System.out.println("LocalDateTime 转 String: "+localDateTime.format(dateTimeFormatter1));

    System.out.println("-------------------------------");

    /** LocalDate 转 LocalDateTime */
    System.out.println("LocalDate 转 LocalDateTime: "+LocalDateTime.of(localDate,LocalTime.parse("00:00:00")));
    /** LocalDate 转 Long */
    System.out.println("LocalDate 转 Long: "+localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli());
    /** LocalDate 转 Date */
    System.out.println("LocalDate 转 Date: "+Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
    /** LocalDate 转 String */
    System.out.println("LocalDateTime 转 String: "+localDateTime.format(dateTimeFormatter2));

    System.out.println("-------------------------------");

    /** Date 转 LocalDateTime */
    System.out.println("Date 转 LocalDateTime: "+LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()));
    /** Date 转 Long */
    System.out.println("Date 转 Long: "+date.getTime());
    /** Date 转 LocalDate */
    System.out.println("Date 转 LocalDateTime: "+LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate());
    /** Date 转 String */
    SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS" );
    System.out.println("Date 转 String: "+sdf.format(date));

    System.out.println("-------------------------------");

    /** String 转 LocalDateTime */
    System.out.println("String 转 LocalDateTime: "+LocalDateTime.parse(strDateTime,dateTimeFormatter1));
    /** String 转 LocalDate */
    System.out.println("String 转 LocalDate: "+LocalDateTime.parse(strDateTime,dateTimeFormatter1).toLocalDate());
    System.out.println("String 转 LocalDate: "+LocalDate.parse(strDate,dateTimeFormatter2));
    /** String 转 Date */
    System.out.println("String 转 Date: "+Date.from(LocalDateTime.parse(strDateTime,dateTimeFormatter1).atZone(ZoneId.systemDefault()).toInstant()));

    System.out.println("-------------------------------");

    /** Long 转 LocalDateTime */
    System.out.println("Long 转 LocalDateTime:"+LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()));
    /** Long 转 LocalDate */
    System.out.println("Long 转 LocalDate:"+LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()).toLocalDate());

  • 相关阅读:
    Linux安装MySQL5.7.25
    Linux配置jdk
    SpringBoot junit 测试
    SpringBoot jasypt 对配置文件项加密
    关于Hibernate级联操作的总结
    GROUP BY 和 ORDER BY一起使用时,要注意的问题!
    web.xml 中的listener、 filter、servlet 加载顺序及其详解
    Hibernate对象的生命周期(补充)
    hibernate--持久对象的生命周期介绍
    开发属于你自己的标签库!
  • 原文地址:https://www.cnblogs.com/puke/p/11314431.html
Copyright © 2011-2022 走看看