Java時間和時間戳的相互轉換

本文轉載自https://www.cnblogs.com/mstk/p/5511057.html,若有侵權聯繫刪除!html

時間轉換爲時間戳:spa

 

   /* 
     * 將時間轉換爲時間戳
     */    
    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;
    }

 

 

 

時間戳轉換爲時間:code

 

   /* 
     * 將時間戳轉換爲時間
     */
    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;
    }
相關文章
相關標籤/搜索