time:2020年1月10日17:24:45 type: 代碼筆記
文章目錄
時間戳
時間戳
是指格林威治時間1970年01月01日00時00分00秒(北京時間1970年01月01日08時00分00秒)起至現在的總秒數。
通俗的講, 時間戳是一份能夠表示一份數據在一個特定時間點已經存在的完整的可驗證的數據。
// 獲取java 的毫米級別的時間,
System.currentTimeMillis();
System.nanoTime() // 納秒級別的時間,返回的時間
/*
* 將時間轉換爲時間戳
*/
public static String dateToStamp(String s) throws ParseException{
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(s);
long ts = date.getTime();
res = String.valueOf(ts);
return res;
}
/*
* 將時間戳轉換爲時間
*/
public static String stampToDate(String s){
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lt = new Long(s);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}