简体   繁体   中英

How get timezone offset from spring request?

I have class:

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

    }
  }

How I can get Timezone offset with this request? It's possible?

It writes a date to update a user, but needs to save a time zone, or rather its offset, to correctly present the result.

    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 have time in milliseconds, eg 3600000 -> +01:00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM