java 代碼添加時間戳

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;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章