1.Date转String
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\"); String dateTime = sdf.format(date);
2.String 转Date
String dateTime = \"2019-01-01 01:01:01\";
SimpleDateFormat sdf = new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\"); try {
Date date = sdf.parse(dateTime); } catch (ParseException e) { e.printStackTrace(); }
加异常处理是因为String 的格式可能会和 SimpleDateFormat指定的格式不符合,⽐如String dateTime没有时分秒,或者少了年⽉⽇时分秒中的某⼀个,都会抛出ParseException异常2021-05-24更新
@Test
public void Test2(){
Date time =new Date(\"Tue Mar 26 00:00:00 CST 2019\");
SimpleDateFormat sdf = new SimpleDateFormat(\"yyyy-MM-dd\"); String timeFormat = sdf.format(time); System.out.println(timeFormat); }
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- huatuo0.com 版权所有 湘ICP备2023021991号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务