How to set java timezone?

2019-02-14 05:01发布

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)

6条回答
不美不萌又怎样
2楼-- · 2019-02-14 05:44

http://minaret.biz/tips/timezone.html

I am not sure but it might be helpful.

查看更多
地球回转人心会变
3楼-- · 2019-02-14 05:44

use System.getCurrentTimeInMillis();

and then take a calendar instance and set Your time..

查看更多
时光不老,我们不散
4楼-- · 2019-02-14 05:48

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.

查看更多
Root(大扎)
5楼-- · 2019-02-14 05:54

This code helped me.

TimeZone tzone = TimeZone.getTimeZone("Singapore");
// set time zone to default
tzone.setDefault(tzone);
查看更多
疯言疯语
6楼-- · 2019-02-14 06:02

You can use TimeZone.setDefault(..) when your application starts, or pass the timezone as command-line argument: -Duser.timezone=GMT

查看更多
混吃等死
7楼-- · 2019-02-14 06:02
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.

查看更多
登录 后发表回答