zoukankan      html  css  js  c++  java
  • LocalDateTime、LocalDate、Date 转为 String

    LocalDateTime、LocalDate、Date 转为 String

    上篇博客中,介绍了 LocalDateTime、LocalDate 及 Date 的相互转换,另外的场景就是需要把 LocalDateTime、LocalDate 或者是 Date与 String 字符串进行转换。

    LocalDateTime、LocalDate、Date 转String

    //Date转String
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    String dateString = sdf.format(date);
    
    //LocalDateTime转String
    LocalDateTime localDateTime = LocalDateTime.now();
    DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
    String localDateTimeString = dtf1.format(localDateTime);
    
    //LocalDate转String
    LocalDate localDate = LocalDate.now();
    DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    String localDateString = dtf2.format(localDate);
    

    String 转 Date、LocalDateTime、LocalDate

    //String转Date
    String dateString = "2020-10-27 14:53:00";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    Date date = sdf.parse(dateString);
    
    //String转LocalDateTime
    String localDateTimeString = "2020-10-27 18:15:00";
    DateTimeFormatter dtf1 = ofPattern("yyyy-MM-dd hh:mm:ss");
    LocalDateTime localDateTime = LocalDateTime.parse(localDateTimeString, dtf1);
    
    //String转LocalDate
    String localDateString = "2020-10-27";
    DateTimeFormatter dtf2 = ofPattern("yyyy-MM-dd");
    LocalDate localDate = LocalDate.parse(localDateString, dtf2);
    
    自我控制是最强者的本能-萧伯纳
  • 相关阅读:
    Highcharts之饼图
    设计模式学习之原型模式
    jQuery学习之结构解析
    JS学习之闭包的理解
    JS学习之prototype属性
    JS学习之事件冒泡
    Mybatis学习之JDBC缺陷
    Spring学习之Aop的基本概念
    Struts学习之值栈的理解
    Struts学习之自定义结果集
  • 原文地址:https://www.cnblogs.com/CF1314/p/13885007.html
Copyright © 2011-2022 走看看