java_时间戳与Date_相互转化

来源:互联网 发布:免费听书软件 编辑:程序博客网 时间:2024/06/04 18:40

标记下来,方便随时用

时间转换为时间戳:

/*      * 将时间转换为时间戳     */        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;    }