码迷,mamicode.com
首页 > 其他好文 > 详细

Date与String之间的转换

时间:2018-03-01 21:39:00      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:for   strong   test   字符   class   except   span   date()   throws   

(1) 将Date格式化为String   

//日期格式化
public void testFormat(){
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
      Date date=new Date();

      String str=sdf.format(date);
      System.out.println(str);
}

 

(2) 将String解析为Date  

//和format方法相反,parse方法用于按照特定格式将表示时间的字符串转换为Date对象
public void testParse() throws Exception{
      String str="2018-01-23";
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

      Date date=sdf.parse(str);
      System.out.println(date);  
}

 

Date与String之间的转换

标签:for   strong   test   字符   class   except   span   date()   throws   

原文地址:https://www.cnblogs.com/nancyzhang/p/8490346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!