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

时间转换为时间戳:    /*       * 将时间转换为时间戳     */       &nb...

时间转换为时间戳:

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


时间戳转成时间Date对象

public static Date stampToDate(String s){
        Long time = 1500435950L;
        Date d = new Date();
        d.setTime(time);
        return d;
    }

  • 发表于 2017-07-19 11:40
  • 阅读 ( 494 )
  • 分类:Java基础

条评论

请先 登录 后评论
不写代码的码农
三叔

66 篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除