Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how TimeZone.getDefault() works?

Tags:

java

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...

def dateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT,Locale.getDefault())
dateFormat.setTimeZone(TimeZone.getDefault())
like image 454
NARENDRA Avatar asked Feb 15 '13 13:02

NARENDRA


People also ask

What is TimeZone getDefault ()?

The getDefault() method of TimeZone class in Java is used to know the default TimeZone for this system or host. This may vary in according to the implementation in different environment. Syntax: public static TimeZone getDefault() Parameters: The method does not take any parameters.

How does JVM get TimeZone?

By default, the JVM reads time zone information from the operating system. This information gets passed to the TimeZone class, which stores the time zone and calculates the daylight saving time. We can call the method getDefault, which will return the time zone where the program is running.

How does Java handle time zones?

If you cannot change the OS or the JVM timezone, you can still convert a Java Date/Time or Timestamp to a specific time zone using the following two JDBC methods: PreparedStatement#setTimestamp(int parameterIndex, Timestamp x, Calendar cal) – to convert the timestamp that goes to the database.

What is offset in TimeZone in Java?

The OffsetTime class, in effect, combines the LocalTime class with the ZoneOffset class. It is used to represent time (hour, minute, second, nanosecond) with an offset from Greenwich/UTC time (+/-hours:minutes, such as +06:00 or -08:00).


2 Answers

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".

like image 117
WPrecht Avatar answered Oct 16 '22 09:10

WPrecht


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 +%Z in Unix console on server.

like image 41
Mikhail Vladimirov Avatar answered Oct 16 '22 09:10

Mikhail Vladimirov