String str = "2022-04-21 14:10:28";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
Date date = simpleDateFormat.parse(str);
// 将日期格式转为时间戳
long result1 = date.getTime();
System.out.println(result1);
long now =System.currentTimeMillis(); // 当前时间戳
// 将当前时间戳转为日期格式
String result2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now);
System.out.println(result2);
java“时间戳“和“日期“相互转换
最新推荐文章于 2024-07-15 03:36:15 发布
这段代码演示了如何将字符串日期转换为时间戳,以及将当前时间戳转换回日期格式。使用了SimpleDateFormat类进行日期格式化操作。
1302

被折叠的 条评论
为什么被折叠?



