简体   繁体   中英

Is it possible to get the user's timezone in a JHipster app?

I have the following code in my JHipster app. It gives me the correct timezone when I'm running locally on my laptop.

@RequestMapping(value = "/points-this-week",
    method = RequestMethod.GET,
    produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<PointsPerWeek> getPointsThisWeek(TimeZone timeZone) {
    DateTimeZone usersTimeZone = DateTimeZone.forID(timeZone.getID());
    log.debug("Getting points for week with timezone: {}", usersTimeZone);

    // Get current date
    LocalDate now = new LocalDate(usersTimeZone);

However, when running on Heroku, timeZone.getID() is "Etc/UTC". Is it possible to get the client's timezone in a JHipster app?

TimeZone timeZone holds the current server's timezone not the client timezone. You can easily get the client locale but for client timezone you should do more things. By default timezone information is not passed in the http headers. You can get the client timezone from the browser and store it in the request header or in the Cookie and then on your server you will have easy access to client timezone. this is a helpful library.

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