java TimeZone.getDefault() 如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14895739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:45:19  来源:igfitidea点击:

how TimeZone.getDefault() works?

java

提问by NARENDRA

My server is in US and I am accessing the application in India through web browser, in that case what TimeZone.getDefault()will return? If it returns Time Zone based on India on basis of what it will return?

I have changed in control panel setting to different locale and different time zone of the system even though it is not changing based on my settings.

I have written the code as fallows...

我的服务器在美国,我正在通过网络浏览器访问印度的应用程序,在这种情况下TimeZone.getDefault()会返回什么?如果它返回基于印度的时区,它将返回什么?

我已将控制面板设置更改为系统的不同语言环境和不同时区,即使它没有根据我的设置进行更改。

我已将代码编写为休耕......

def dateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT,Locale.getDefault())
dateFormat.setTimeZone(TimeZone.getDefault())

采纳答案by WPrecht

It's going to return the timezone of the JVM TimeZone.getDefault() is executed on. So if the application is running on a server in India, it will be something like "Asia/Calcutta".

它将返回执行 JVM TimeZone.getDefault() 的时区。因此,如果应用程序在印度的服务器上运行,它将类似于“Asia/Calcutta”。

回答by Mikhail Vladimirov

Default time zone is usually set for host, not user or application. In your case it will be default time zone for a server where your application is running, most probably US time zone.

默认时区通常是为主机设置的,而不是为用户或应用程序设置的。在您的情况下,它将是您的应用程序正在运行的服务器的默认时区,很可能是美国时区。

Try to run command date +%Zin Unix console on server.

尝试date +%Z在服务器上的 Unix 控制台中运行命令。

回答by n0rm1e

This is a matter of 'where this code executes'. If you are talking about a web application which is being accessed by a browser in India, the kind of date you get will be in US. Well, unless you set the proper locale and timezone for the user's session. In frameworks like Vaadinonce you call setLocale()on the application it sets the timezone as well, but in other platforms you might have to explicitly use a date formatter with a specific timezone.

这是“此代码在哪里执行”的问题。如果您谈论的是印度的浏览器正在访问的 Web 应用程序,那么您获得的日期类型将是美国。好吧,除非您为用户会话设置了正确的语言环境和时区。在像Vaadin这样的框架中,一旦您调用setLocale()应用程序,它也会设置时区,但在其他平台中,您可能必须明确使用具有特定时区的日期格式化程序。