zoukankan      html  css  js  c++  java
  • java中的日期类型之间转换

    一、String与Date(java.util.Date)的转换

       1、String--->Date

        String str="2014/1/11 12:34:25";

        Date date=new Date();

        DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");  //这里规定时间的格式要与String时间类型的格式相同

        date=sdf.parse(str);//date类型

      2、Date--->String

        Date date=new Date();

        DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");  //Date转String时,这里的格式随意

        String tr=sdf.format(date);

    二、String与Timestamp的转换

      1、String--->Timestamp

          String str="2014/1/11 12:34:25";

          Timestamp ts=new Timestamp();

          ts=Timestamp.valueOf(str);

      2、Timestamp--->String

          方法一:

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

          String str=ts.toString();

          方法二:

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

          String str=sdf.format(ts);

    三、Date(java.util.Date)与Timestamp的转换

      1、Timestamp--->Date

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

          Date date=new Date();

          date=ts;

      2、Date--->Timestamp

          父类不能直接转换成子类,可以先转成String后,在转Timestamp

          Date date=new Date();

          Timestamp ts=new Timestamp(date.getTime());

    四、long与Timestamp的转换

      1、long--->Timestamp

          long l="";

          new Timestamp(l);

      2、Timestamp--->long

          Timestamp ts=new Timestamp();

          long now=ts.getDateTime();

  • 相关阅读:
    中文词频统计
    复合数据类型,英文词频统计
    Mybatis 异常:Cause: java.io.IOException: Could not find resource com.xxx.xxx.xml
    Ajax:修改了项目的ajax相关代码,点击运行没有效果
    大数据应用期末总评
    分布式并行计算MapReduce
    分布式文件系统HDFS 练习
    安装关系型数据库MySQL和大数据处理框架Hadoop
    爬虫综合大作业
    爬取全部的校园新闻
  • 原文地址:https://www.cnblogs.com/CodingAndRiding/p/8108803.html
Copyright © 2011-2022 走看看