【问题标题】:How get timezone offset from spring request?如何从春季请求中获取时区偏移量?
【发布时间】:2017-07-07 11:27:59
【问题描述】:

我有课:

    @RestController
    @RequestMapping("/api/user")
    public class UserCtrl {
      @RequestMapping(value = "/update", method = RequestMethod.POST)
      public ResponseEntity updateUser(@RequestBody StringModel updateModel) {

    }
  }

如何通过此请求获得时区偏移量?有可能吗?

它写入一个日期来更新用户,但需要保存一个时区,或者更确切地说是它的偏移量,以正确呈现结果。

【问题讨论】:

    标签: java spring request timezone timezone-offset


    【解决方案1】:
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        Locale locale = request.getLocale();
        Calendar calenda = Calendar.getInstance(locale);
        TimeZone timeZone = calenda.getTimeZone();
        int rawOffset = timeZone.getRawOffset();
        ZoneId zoneId = timeZone.toZoneId();
        ZoneRules rules = zoneId.getRules();
    

    rawOffset 以毫秒为单位,例如 3600000 -> +01:00

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-20
      • 2021-04-11
      • 2016-08-06
      • 2020-04-20
      • 1970-01-01
      • 2017-02-20
      • 2016-04-22
      • 2014-03-27
      相关资源
      最近更新 更多