java的日期时间处理

1.  /*
       * 将时间转换为时间戳
       */   
    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);//注意s的格式
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

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

猜你喜欢

转载自www.cnblogs.com/ZeGod/p/9953254.html