如何从语言环境中获取当前时间和时区?
我有一个奇怪的问题,当我创建一个带有语言环境的日历时,时区只是恢复到本地的一个
I have this weird problem, when I create a calendar with a locale, the TimeZone just reverts to the local one
public void start(Locale locale){
String s = locale.getDisplayName();
System.err.println(s);
Calendar c = new GregorianCalendar(locale);
System.err.println(c.getTimeZone());
}
这是输出:
español (Argentina)
sun.util.calendar.ZoneInfo[id="Europe/Bucharest", //etc more useless date here....
我怎样才能从特定的语言环境中获得正确的时间?
How can i get the proper time from a specific locale ?
推荐答案
简短的回答:你不能.
长答案:没有适合地区的时区"之类的东西.那只是因为有几个国家有多个时区(例如美国).时区是一个不同的概念.
The long answer: There is no such thing as "proper time zone for a locale". That's just because there are a few countries that have more than one time zone (for example United States). Time zone is a different concept.
无论如何,您正在寻求解决您的问题.我猜您正在编写一个 Web 应用程序,并且您看到时区正在恢复为服务器默认值.这是典型的情况.Locale.getDefault()
和 TimeZone.getDefault()
都会返回与服务器相关的信息.JVM 无法知道正确的"时区.那么你能做些什么呢?
Anyway, you are looking to solve your problem. I am guessing that you are writing a web application and you see that the time zone is reverting to the server default. That's a typical situation. Both Locale.getDefault()
and TimeZone.getDefault()
will return server-related information. The JVM has no way of knowing the "proper" time zone. So what can you do about it?
- 您可以将时区信息添加到用户配置文件(如果有),或创建时区组合框(以便用户可以在运行时切换).然后你可以为
DateFormat
实例分配一个合适的对象,它会自动转换时区. - 您可以通过 JavaScript 日期对象的 当前时区偏移量.asp" rel="noreferrer">getTimezoneOffset() 函数并以某种方式(AJAX)将其发送到服务器.这种方法的问题是有几个时区具有该偏移量,并且所选时区可能不适合其他日期.当然,您可以通过轮询时间变化日期前后的数据来猜测时区,但这可能不是您想要做的.
- 您可以将未格式化的时间发送给客户端(例如写为 ISO 8601 日期时间格式 或作为 Unix 纪元时间 与 UTC 相关)并具有 全球化 或 Dojo为您设置日期和时间格式.
- You can add time zone information to user profile (if you have one), or create a time zone combo box (so that the user can switch at runtime). Then you can assign an appropriate object to
DateFormat
instance and it will convert time zone automatically. - You can read the current time zone offset from the client via JavaScript Date Object's getTimezoneOffset() function and somehow (AJAX) send it to the server. The problem with this method is that there are several time zones with that offset, and the selected time zone might be inappropriate for other dates. Of course you can guess the time zone by polling the data around time change date, but this is probably not what you want to do.
- You can send the unformatted time to the client (for example written as ISO 8601 date-time format or as a Unix time of the epoch in relation to UTC) and have Globalize or Dojo format date and time for you.
在这三个可能的选择中,我总是选择第一.通过将时区信息放入用户个人资料中,您可以确定他/她的首选时区是什么,而不管他们当前的网络浏览器等.请保持请注意,有些用户可能希望在访问其他国家/地区时使用您的应用程序...
Out of these three possible choices, I always opt for number 1. By putting time zone information to user profile, you know for sure what his/her preferred time zone is, regardless of their current web browser, etc. Please keep in mind that some users might want to use your application while visiting other countries...
相关文章