java时间戳正确,时间显示错误问题

从数据库取出时间戳为十位int类型,精确到秒的时间戳

web页面拿到显示为“yyyy-MM-dd hh:mm:ss”为变成“1970-01-01 20:20:20”,与数据库存入的当时时间,相差了几十年

仔细观察是转换为"long"型时出错:如图

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        long signtime = auser.getLastsigntime()*1000;
        String lastSignTime = dateFormat.format(new Date(signtime));

        req.setAttribute("auser",auser);
        req.setAttribute("addTimes",userAddTimes);
        req.setAttribute("lastSignTime",lastSignTime);
        req.getRequestDispatcher("/userDetail.jsp").forward(req,resp);

"long signtime = auser.getLastsigntime()*1000;"等号右边为int型,int乘以1000,还是int,只不过会越界,并没有转为精度更高的long型,所以signtime实际上没有转化为“时间戳-秒”对应的“时间戳-毫秒”

改成以下就好了:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        long signtime = ((long)(auser.getLastsigntime()))*1000;
        String lastSignTime = dateFormat.format(new Date(signtime));

        req.setAttribute("auser",auser);
        req.setAttribute("addTimes",userAddTimes);
        req.setAttribute("lastSignTime",lastSignTime);
        req.getRequestDispatcher("/userDetail.jsp").forward(req,resp);

在“*1000”乘以1000这个操作以前,就把lastsigntime转化为“long”类型,乘以1000后,也不会越界,就是时间戳葱“秒”换算成“毫秒”了。

小问题,记一下.

更多问题,加qq群:565191947

 

另外,给大家介绍一款国内很实惠便宜的服务器运营商:

https://www.didiyun.com/activity.html?invite=298tSgEjvtp#annually

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值