简体   繁体   English

如何向用户显示正确的时区?

[英]How to show the correct timezone to users?

I have a Java SE server application with a Saas website and registered users. 我有一个带有Saas网站和注册用户的Java SE服务器应用程序。

I have many events that occur on my server in different days. 我有不同日期在服务器上发生的许多事件。

Time is registered in localhost in a long number via SYSTEM.currentTimeMillis()/1000 通过SYSTEM.currentTimeMillis()/1000在本地主机中大量注册了时间

Registered users can check these events time from their respective country and they need to see the correct time based on their timezone (not server timezone) through the website. 注册用户可以从各自的国家/地区查看这些事件的时间,他们需要根据时区(而非服务器时区)通过网站查看正确的时间。

How do I show them the historical time of the events in their timezone ? 如何向他们显示事件所在时区的历史时间? Any idea about how you would deal with this situation? 关于您将如何处理这种情况的任何想法?

Easiest way is to use http://momentjs.com/timezone/ . 最简单的方法是使用http://momentjs.com/timezone/ Idea is following - you send sth like this in html markup 想法紧随其后-您在html标记中发送了类似的内容

<div class="raw-datetime">2014-12-01 12:00:00 UTC+03:00</div>

And after page loads - you run javascript that adjusts all raw datetime to browser timezone. 在页面加载后-您运行的javascript会将所有原始日期时间调整为浏览器时区。

First of all, we will use UTC as a default and unique time zone in the system. 首先,我们将使用UTC作为系统中的默认唯一时区。 Because, the time never go back or go to the future in UTC. 因为,时间永远不会过去或回到UTC的未来。 There is no time shift for daylight saving. 夏令时没有时间偏移。 So, all applications (applications which we develop and has timezone support for their users) environment require a JVM parameter, which provides a UTC based environment . 因此,所有应用程序(我们开发并为其用户提供时区支持的应用程序)环境都需要一个JVM参数,该参数提供基于UTC的环境

Timezone JVM parameter usage 时区JVM参数用法

-Duser.timezone=UTC

For Views 对于视图

For views, the date/time object should be rendered according to the specified time-zone. 对于视图,应根据指定的时区呈现日期/时间对象。 For Java world, this is handled by formatDate tld in jstl . 对于Java的世界,这是由处理formatDate TLD在jstl Every project contains its own timezone holding logic itself. 每个项目本身都包含自己的时区保持逻辑。

User.timeZone : for admin panels User.timeZone:用于管理面板

Some fmt:formatDate example 一些fmt:formatDate示例

<fmt:formatDate value="${someDateTime}" timeZone="${user.timeZone}" pattern="MMM dd, yyyy - HH:mm:ss.S"/>

For further information about formatDate taglib please see check the link or google it. 有关formatDate taglib的更多信息,请查看链接或用Google链接。

For Forms (Getting the date / time data from the user) 对于表单(从用户那里获取日期/时间数据)

When you getting date information via forms the you need to consider time zone and perform the time conversion. 通过表格获取日期信息时,您需要考虑时区并执行时间转换。 The conversion direction is User Time zone to UTC. 转换方向是用户时区到UTC。 The time in database should be in UTC time zone. 数据库中的时间应为UTC时区。

Motto is save it globally; 座右铭在全球范围内保存; show it locally :) 在本地显示:)

EDIT: 编辑:

Hold timezone as a subfield of User object and set it to your formatter when you want to show the time, you can use it in JavaSE . 将时区保留为User对象的子字段,并在要显示时间时将其设置为格式化程序,可以在JavaSE使用它。

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm");
        formatter.setTimeZone(TimeZone.getTimeZone(user.getTimeZone()));

java.time java.time

First you need to determine the user's time zone. 首先,您需要确定用户的时区。 Search on StackOverflow to learn that ultimately the best way to do that is to ask the user. 在StackOverflow上搜索以了解最终做到这一点的最佳方法是询问用户。 You can try to use JavaScript on the browser to auto-detect a time zone, but there are issues. 您可以尝试在浏览器上使用JavaScript来自动检测时区,但是存在问题。

You need to arrive at a proper time zone name , usually a continent/cityOrRegion such as America/Montreal or Asia/Kolkata . 您需要输入正确的时区名称 ,通常是一个洲/城市或Asia/Kolkata例如America/MontrealAsia/Kolkata Never use those 3-4 letter codes like EST or IST as they are neither standardized nor unique. 切勿使用诸如ESTIST这样的3-4个字母代码,因为它们既不标准化也不独特。

To localize a date-time you need to know the user's Locale, a human language and a set of cultural norms. 要本地化日期时间,您需要了解用户的语言环境,人类语言和一系列文化规范。

With a time zone in hand, use the new java.time framework built into Java 8 and later. 掌握了时区之后,请使用Java 8及更高版本中内置的新java.time框架。 Inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project. 受Joda-Time启发,由JSR 310定义,并由ThreeTen-Extra项目扩展。

long epochSeconds = … ;
Instant instant = Instant.ofEpochSeconds( epochSeconds );
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant, zoneId);
Locale locale = Locale.CANADA_FRENCH;
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( locale );
String output = zdt.format( formatter );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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