SimpleDateFormat转换时间格式
SimpleDateFormat有两个常用的方法parse和format
其中SimpleDateFormat在创建时有一下集中格式可以选择
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd")
* 支持格式为 yyyy.MM.dd G 'at' hh:mm:ss z 如 '2002-1-1 AD at 22:10:59 PSD'<br>
* yy/MM/dd HH:mm:ss 如 '2002/1/1 17:55:00'<br>
* yy/MM/dd HH:mm:ss pm 如 '2002/1/1 17:55:00 pm'<br>
* yy-MM-dd HH:mm:ss 如 '2002-1-1 17:55:00' <br>
* yy-MM-dd HH:mm:ss am 如 '2002-1-1 17:55:00 am' <br>
其中parse方法是将将String类型的字符串转换成Date类型的数据
而Format则是将Date类型是数据转换为stringbuffer类型的数据
在网上找了相关的两个小例子:
String str = "2013-01-21 15:10:20";
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd-HH:mm:ss");
date = sdf.parse(str);
--------------------------------------------
SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
String a=dateformat.format(new Date());
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步