admin管理员组文章数量:1291616
I'm sending the user's timezone from frontend in a cookie like this:
document.addEventListener("DOMContentLoaded", () => {
const currentTZ = Intl.DateTimeFormat().resolvedOptions().timeZone;
const cookies = document.cookie.split("; ").reduce((acc, cookie) => {
const [key, value] = cookie.split("=");
acc[key] = value;
return acc;
}, {});
if (!cookies["COOKIE.TZ"]) {
document.cookie = `COOKIE.TZ=${currentTZ}; path=/`;
fetch(`/?zone=${encodeURIComponent(currentTZ)}`, {
method: "GET",
});
} else if (cookies["COOKIE.TZ"] !== currentTZ) {
document.cookie = `COOKIE.TZ=${currentTZ}; path=/`;
fetch(`/?zone=${encodeURIComponent(currentTZ)}`, {
method: "GET",
});
}
});
I want the date to automatically convert to the user's time zone because I now put it in the GlobalControllerAdvice
@ModelAttribute("userZoneId")
public ZoneId getUserZoneId(HttpServletRequest request) {
String timeZoneId = getCookieValue(request, "COOKIE.TZ");
return (timeZoneId != null) ? ZoneId.of(timeZoneId) : ZoneId.of("Europe/Moscow");
}
private String getCookieValue(HttpServletRequest request, String name) {
if (request.getCookies() != null) {
for (Cookie cookie : request.getCookies()) {
if (cookie.getName().equals(name)) {
return cookie.getValue();
}
}
}
return null;
}
and then I use it explicitly
<td th:text="${#temporals.format(user.dateCreated, 'dd-MM-yyyy HH:mm', userZoneId)}"></td>
and it works, but I want to automatically convert to timezone and print only:
<td th:text="${#temporals.format(user.dateCreated, 'dd-MM-yyyy HH:mm')}"></td>
Without using GlobalControllerAdvice. Where to specify and save time zone?
本文标签: springWhere should I store the user39s timezoneStack Overflow
版权声明:本文标题:spring - Where should I store the user's timezone? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741538097a2384150.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论