从数据库取出时间戳为十位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