您的位置:首页 > 编程语言 > Java开发

Java时间和时间戳的相互转换

2017-12-06 16:43 393 查看
时间转换为时间戳:

     /* 

     * 将时间转换为时间戳

     */    

    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;

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: