zoukankan      html  css  js  c++  java
  • java Timestamp、Date和String之间的互转

    1.String 转 Date

    String dateStr = "2012-12-31 00:00:00";

    Date date = new Date();

    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  

    date = sdf.parse(dateStr);

    2.Date 转 String

    Date date = new Date();

    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  

    String dateStr = sdf.format(date);

    3.String 转Timestamp

    String tsStr = "2012-12-31 00:00:00";

    Timestamp ts = new Timestamp(System.currentTimeMillis());

     ts = Timestamp.valueOf(tsStr);

    4.Timestamp 转 String

    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    Timestamp ts = new Timestamp(System.currentTimeMillis());         

    String tsStr = ""

    tsStr = sdf.format(ts); 

    5.Timestamp 转 Date

    Timestamp ts = new Timestamp(System.currentTimeMillis()); 

    Date date = new Date();

    date = ts;

    6.Date 转 Timestamp

    Date date = new Date();
    String time = "";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    time = sdf.format(date);
    Timestamp ts = Timestamp.valueOf(time);

  • 相关阅读:
    Python_异常处理
    Python_文件相关操作
    Python_集合
    Python_字典
    Python_元组
    Python_列表操作2
    Python_列表操作1
    Spring-Data-Jpa使用总结
    SpringBoot源码分析之---SpringBoot项目启动类SpringApplication浅析
    RESTful API批量操作的实现
  • 原文地址:https://www.cnblogs.com/loveLearning/p/2851603.html
Copyright © 2011-2022 走看看