Linux 如何设置java时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7026182/
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
How to set java timezone?
提问by tsds
My system time differs from what java's new Date() tells (+ 4 hours),
so I think it's because some java settings.
How can I make java time to be always as my linux system time?
(by editing some configuration file)
我的系统时间与 java 的 new Date() 告诉的不同(+ 4 小时),
所以我认为这是因为一些 java 设置。
如何让 java 时间始终作为我的 linux 系统时间?
(通过编辑一些配置文件)
回答by asgs
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
Date myDate = calendar.getTime();
System.out.println(myDate);
Is this code printing the right date/time? Else, there's some other problem.
此代码是否打印了正确的日期/时间?否则,还有其他问题。
回答by Bozho
You can use TimeZone.setDefault(..)
when your application starts, or pass the timezone as command-line argument: -Duser.timezone=GMT
您可以TimeZone.setDefault(..)
在应用程序启动时使用,或将时区作为命令行参数传递:-Duser.timezone=GMT
回答by ngesh
use System.getCurrentTimeInMillis();
用 System.getCurrentTimeInMillis();
and then take a calendar instance and set Your time..
然后拿一个日历实例并设置你的时间..
回答by Nick Long
If you want to change the time zone programmatically then you can use:
如果要以编程方式更改时区,则可以使用:
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
I prefer this method because it doesn't rely on people remembering to run my code with the correct time zone arguments.
我更喜欢这种方法,因为它不依赖于人们记得使用正确的时区参数运行我的代码。
回答by Andrew
This code helped me.
这段代码帮助了我。
TimeZone tzone = TimeZone.getTimeZone("Singapore");
// set time zone to default
tzone.setDefault(tzone);
回答by Ole V.V.
Avoid the need
避免需要
Make your date and time code independent of the JVM's time zone setting. The setting is unreliable anyway, so not using it will be for the best regardless.
使您的日期和时间代码独立于 JVM 的时区设置。无论如何,该设置是不可靠的,因此无论如何不使用它是最好的。
For a time zone.
对于一个时区。
ZoneId z = ZoneId.of( "Asia/Japan" ) ; // Or use `ZoneId.systemDefault()` for the JVM's current default time zone. The JVM's default may or may not match that of the host OS' current default time zone.
ZonedDateTime zdt = ZonedDateTime.now( z ) ; // Capture the current moment as seen in a particular time zone.
For UTC (an offset of zero).
对于 UTC(零偏移量)。
OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ) ;
Yes, I know that printing an old-fashioned java.util.Date
and thereby implicitly invoking its toString
method causes it to use the time zone setting for rendering the string to be printed. You should no longer use the Date
class, though. It's poorly designed. The confusing trait of using the JVM time zone and thus pretending that a Date
holds a time zone is just one of its many bad sides. Use java.time, the modern Java date and time API, for your date and time work.
是的,我知道打印老式java.util.Date
并隐式调用其toString
方法会导致它使用时区设置来呈现要打印的字符串。不过,您不应再使用Date
该类。它的设计很差。使用 JVM 时区并因此假装 aDate
持有时区的令人困惑的特征只是其众多不利方面之一。使用现代 Java 日期和时间 API java.time 进行日期和时间工作。
If you can't avoid the need
如果你无法避免需要
Set the environment variable TZ
in Unix to the desired time zone ID. For example this worked for me on my Mac:
将TZ
Unix 中的环境变量设置为所需的时区 ID。例如,这在我的 Mac 上对我有用:
export TZ=Etc/UTC
java -cp /Your/class/path your.package.YourMainClass
Link
关联
Oracle tutorial: Date Timeexplaining how to use java.time.
Oracle 教程:解释如何使用 java.time 的日期时间。