java中时间与时间戳的转换及去掉时间中的空格及横杠、冒号

 

时间转换为时间戳:

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

 

附:java时间字符串去空格、冒号和横杠

String date = "2017-09-19 14:40:01";
String response = date.replaceAll("[[\\s-:punct:]]","");

 


版权声明:本文为qq_35673617原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。