How to set java timezone?

My system time is different from what java new Date () (+ 4 hours) reports
so I think this is due to some java settings.
How can I make java time always be like my Linux system time?
(by editing some configuration file)

+4
java timezone linux time
source share
6 answers

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

I'm not sure, but may be helpful.

+4
source share

You can use TimeZone.setDefault(..) when starting the application or pass the time zone as a command line argument: -Duser.timezone=GMT

+16
source share

If you want to change the time zone programmatically, you can use:

 TimeZone.setDefault(TimeZone.getTimeZone("UTC")) 

I prefer this method because it does not rely on people who remember that I run my code with the correct time zone arguments.

+3
source share
 Calendar calendar = Calendar.getInstance(TimeZone.getDefault()); Date myDate = calendar.getTime(); System.out.println(myDate); 

Does this code print the correct date / time? In addition, there is another problem.

+2
source share

This code helped me.

 TimeZone tzone = TimeZone.getTimeZone("Singapore"); // set time zone to default tzone.setDefault(tzone); 
+2
source share

use System.getCurrentTimeInMillis();

and then take a copy of the calendar and set the time.

0
source share

All Articles