云迈博客

您现在的位置是:首页 > 后端开发 > Java > 正文

Java

Java时间转换:标准化日期格式与毫秒时间戳互相转换

凌玄龙2021-07-30Java413
1:分享的是标准化日期格式与毫秒时间戳互相转换,采用SimpleDateFormat类进行核心代码的实现,以及转换。2:代码示例:`importjava.text.ParseExceptio

1:分享的是标准化日期格式与毫秒时间戳互相转换,
采用SimpleDateFormat类进行核心代码的实现,以及转换。

2:代码示例:

`import java.text.ParseException;
import java.text.SimpleDateFormat;

public class dateFormatUtil {
public static void main(String[] args) throws ParseException {
long time = System.currentTimeMillis();
System.out.println(time);
System.out.println(timeToFormat(time));
System.out.println(timeToSecond(timeToFormat(time)));
}
//13位毫秒时间戳 –> yyyy-MM-dd HH mm ss
public static String timeToFormat(long time) {
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH mm ss”);//设置日期格式
return sdf.format(time);
}
//yyyy-MM-dd HH mm ss –> 13位毫秒时间戳
public static long timeToSecond(String date) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH mm ss”);
return sdf.parse(date).getTime();
}
}
`

发表评论

评论列表

  • 这篇文章还没有收到评论,赶紧来抢沙发吧~