日期时间与unix时间戳互转

时间:2021-12-24 22:06:22
1、时间---->时间戳
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("时间戳:"+String.valueOf(sdf.parse("2017-05-18 08:52:55").getTime() / 1000));

2、时间戳---->时间
Long timestamp = Long.parseLong("1495068775") * 1000;
System.out.println("时间:"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(timestamp)));


 
控制台输出:
 
日期时间与unix时间戳互转