加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – Spring应用程序显示服务器时间而不是客户端时间

发布时间:2020-12-15 01:40:45 所属栏目:大数据 来源:网络整理
导读:这是我的代码: @RequestMapping(value="/test",method=RequestMethod.GET)public @ResponseBody String test(HttpServletRequest request) { Calendar calendar = new GregorianCalendar(request.getLocale()); String currentTime = calendar.getTime().to

这是我的代码:

@RequestMapping(value="/test",method=RequestMethod.GET)
public @ResponseBody String test(HttpServletRequest request) {
    Calendar calendar = new GregorianCalendar(request.getLocale());
    String currentTime = calendar.getTime().toString();
    return"Current Time: " + currentTime;
}

这次向我展示:

Current Time: Fri Nov 30 22:45:42 UTC 2012

我在中央时区,所以应该告诉我这个:

Current Time: Fri Nov 30 14:45:42 CST 2012

为什么我得到服务器时间而不是客户端时间?

最佳答案
无论客户端在哪里,您的代码都会在服务器上执行.在服务器上,您将为该计算机设置时区,该时区将用于计算时间.当你调用calendar.getTime()时,将使用这个时区.

如果您需要客户端时区,则需要发送它并使用SimpleDateFormat将服务器时间转换为客户端时区.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读