java 時間,時間戳互轉

   /**
     * 時間轉換成時間戳
     * @param time
     * @return
     */
    public static long dateToTimestamp(String time){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = simpleDateFormat.parse(time);
            long ts = date.getTime() / 1000;
            return ts;
        } catch (ParseException e) {
            return 0;
        }
    }
注:必定要寫try, catch,否則會報unhandled exception: java.text.ParseException的錯誤

/** * 時間戳轉時間(11位時間戳) * @param time * @return */public static String timestampToDate(long time) {    String dateTime;    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT_SEC_FULL);    long timeLong = Long.valueOf(time);    dateTime = simpleDateFormat.format(new Date(timeLong * 1000L));    return dateTime;}
相關文章
相關標籤/搜索