Java时间戳和时间格式的互转
1. 时间戳转换成时间
/**
* 时间戳转换成时间
*/
@Test
public void demo01() {
/** 时间戳 */
String s = "1545098699000";
long it = new Long(s);
Date date = new Date(it);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String res = simpleDateFormat.format(date);
System.out.println(res);
}
2. 时间转换成时间戳
/**
* 时间转成时间戳
*/
@Test
public void demo02() throws ParseException {
/** 时间 */
String res = "2018-12-18 10:04:59";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(res);
long it = date.getTime();
String s = String.valueOf(it);
System.out.println(s);
}