String和Date的相互转化
2015-09-07 11:01
232 查看
一、String转Date
其中String time一定要与simpleDateFormate的格式一致,不然会报Unparseable date的异常
二、Date转String
三、String格式的时间转化为另一种String格式
public static Date String2date(String time){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss.SSS"); Date date = null; try { date = sdf.parse(time); } catch (ParseException e) { e.printStackTrace(); } return date; }
其中String time一定要与simpleDateFormate的格式一致,不然会报Unparseable date的异常
二、Date转String
public static String Date2string(Date date){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss.SSS"); String str = sdf.format(date); return str; }
三、String格式的时间转化为另一种String格式
public static String Strdate2another(String str){ SimpleDateFormat oldsdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss.SSS"); SimpleDateFormat newsdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); String newtime = null; try { Date date = oldsdf.parse(str); newtime = newsdf.format(date); } catch (Exception e) { e.printStackTrace(); } return newtime; }其中传入的str格式要和oldsdf一致,然后输出的String的时间newtime格式是和newsdf一致的。
相关文章推荐
- 【LeetCode】P029_DivideTwoIntegers
- MSSql Server基础学习系列———数据修改
- something about the systemverilog--- interface
- Alpha 测试和 beta 测试区别到底在哪儿?
- 如何设置HTML页面自适应宽度的table(表格)
- Eclipse中安装Jetty插件
- C#模拟发送http get、post请求的方式
- AFNetworking的使用
- 个性化WinPE封装方法 ----最后实战“制作WinPE3.0图文教程”
- 个性化WinPE封装方法----制作过程需要了解的“命令”
- 个性化WinPE封装方法----制作过程需要了解的“命令”
- 个性化WinPE封装方法 ----最后实战“制作WinPE3.0图文教程”
- 个性化WinPE封装方法----制作过程需要了解的“命令”
- 个性化WinPE封装方法 ----最后实战“制作WinPE3.0图文教程”
- 查看端口
- win下连接virtualbox中linux虚拟机的mysql
- control
- android 如何获取当前的Activity类名
- emacs菜单栏摸索
- 个性化WinPE封装方法----制作过程需要了解的“命令”