如何从弹簧请求中获取时区偏移量?

时间:2017-07-07 11:28:00

标签: java spring request timezone timezone-offset

我上课了:

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

    }
  }

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

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

1 个答案:

答案 0 :(得分: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