时间戳和date转换


    /**
     * @param s  时间戳
     * @return  date类型
     */
    public static Date timeToDate(String s) {
        long lt = new Long(s);
        Date date = new Date(lt);
        return date;
    }


    /** date 转时间戳
     * @param date
     * @return
     */
    public static Long getatime(Date date){
        return date.getTime();
    }

    /**
     * @param s 时间戳
     * @return  date字符串
     */
    public static String stampToDateString(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;
    }

    public static void main(String[] args) {
        //
        String s = "1546071846000";

        Date date=new Date();
        String toString = date.toString();
        System.out.println(toString);

        long time = date.getTime();
        System.out.println(stampToDateString(s));
        System.out.println(timeToDate(s));
    }

 

你可能感兴趣的:(java,写好的代码)