
image.png
@[toc]
一、介紹
這段代碼演示了如何將指定格式的日期時(shí)間字符串(如"2020-10-11 10:42:01")通過SimpleDateFormat解析為Date對象,再轉(zhuǎn)換為對應(yīng)的毫秒級(jí)時(shí)間戳,常用于日期參數(shù)轉(zhuǎn)換、時(shí)間計(jì)算或數(shù)據(jù)存儲(chǔ)。
二、代碼
//String轉(zhuǎn)Long(日期格式字符串轉(zhuǎn)為時(shí)間毫秒數(shù))
@Test
void stringTransformMilliseconds() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = "2020-10-11 10:42:01";
Date date = null;
try {
date = sdf.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
long time1 = date.getTime();
System.out.println("時(shí)間毫秒數(shù):" + time1);
//輸出:時(shí)間毫秒數(shù):1602384121000
}