format的用法 是将当前时间格式转换为指定格式
场景一:给定毫秒数或者当前系统时间,返回指定时间格式 输入
Date date=new Date();//获得系统当前的时间
// long date=(long)24979599*60000; //任意毫秒数,可以parse转化为日期类型后getTime获取
// long date=1498838705129l;
System.out.println(date);
SimpleDateFormat sd=new SimpleDateFormat(\"yyyy-MM-dd HH:mm\");
String form=sd.format(date);
return form;
注意:在第二行和第三行抓化为long类型时要强制性long转换,否则会提示type int is out of range
场景二:给定任意时间格式,返回毫秒数
parse转化为Date类型后可以直接获取毫秒。输入2017-06-28T09:52 返回毫秒
String s=\"2017-06-28T09:52\";
SimpleDateFormat sdf=new SimpleDateFormat(\"yyyy-MM-dd\'T\'HH:mm\");
Date date=sdf.parse(s);
System.out.println(date);//Sat Jan 28 09:52:00 CST 2017
System.out.println(date.getTime());
场景三:将给定格式转换为指定格式
输入 06-29-2017 输出 2017/06/29 先parse转化为date类型,再将其format为指定日期类型
String str = \"06-29-2017\";
SimpleDateFormat sd = new SimpleDateFormat(\"MM-dd-yyyy\");
Date date = (Date) sd.parse(str);
System.out.println(date);
sd = new SimpleDateFormat(\"yyyy/MM/dd\");
String strDate = sd.format(date);
System.out.println(strDate);
作者:龙骨
来源:CSDN
原文:https://blog.csdn.net/qq_31780525/article/details/73924487?utm_source=copy
版权声明:本文为博主原创文章,转载请附上博文链接!
继续阅读与本文标签相同的文章
上一篇 :
爬虫-xpath 匹配
下一篇 :
物联网竟然有这个大用处!工厂的数字化你了解多少
-
浅析云存储的TCS和LCA两大架构
2026-05-18栏目: 教程
-
虚拟机模拟部署Extended Clusters(五)总结
2026-05-18栏目: 教程
-
Java计算两个日期相差的月数
2026-05-18栏目: 教程
-
精准测试与自动化测试的无缝对接
2026-05-18栏目: 教程
-
Arthas 3.1.2 版本发布 | 增加 logger/heapdump/vmoption 命令
2026-05-18栏目: 教程
