3

I am using Java 7 on a Windows machine. I am developing a web app running on Tomcat.

I need to know the server's time zone. Here is what I do for this purpose:

TimeZone.getDefault();

Is this the correct way?

Thanks for your input!

Regards.

curious1
  • 14,155
  • 37
  • 130
  • 231
  • A java application hosted on a web server runs in the client virtual machine. So you will get the client timezone. – Black Frog Apr 06 '13 at 02:06
  • Hi @BlackFrog, thanks for your input! Do you think that I can get server's time zone by using TimeZone.getDefault()? – curious1 Apr 06 '13 at 14:01
  • @BlackFrog No, a Java web app is based on Servlets, and executes the Java only on the server-side. No Java code runs on the client-side. – Basil Bourque Feb 26 '18 at 05:55

1 Answers1

3

tl;dr

TimeZone.getDefault()

Is this the correct way?

No, that class is now legacy, replaced by ZoneId.

ZoneId.systemDefault()

But better if you specify your desired/expected zone rather than rely on JVM’s current default.

java.time

The modern solution uses the java.time classes (ZoneId, ZoneOffset, and ZoneRules) rather than the troublesome old legacy date-time classes (TimeZone).

Current default

You can ask for the JVM’s current default time zone.

ZoneId z = ZoneId.systemDefault() ;

But whatever purpose you have in mind is likely ill-advised. Firstly, best practice for servers is to set the host OS and the JVM’s default time zone to UTC. Secondly, you should never depend on the server’s time zone. That setting is out of your control as a programmer, and easily changed to an unexpected value. Within a JVM, any code in any thread of any app within that JVM may change the current default time zone during runtime.

Specify zone

Better to always specify your desired/expected time zone by passing an optional argument to the various java.time methods.

ZonedDateTime zdt = ZonedDateTime.now( ZoneId.of( "Africa/Tunis" ) ) ;

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" ) ;  

By the way, ditto for Locale — always specify explicitly rather than rely implicitly on current default.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • In my case, the infra-man (don't know how to describe his post) says that each pod has the correct timezone and that it is his function to guarantee that, that any problem caused by date is a dev-issue, so now here I am removing all `@JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss", shape = JsonFormat.Shape.STRING, timezone = "America/Sao_Paulo")` accross all microservices. Newbie question: Is this zoneId the default for all date instances? – Washington Guedes Nov 19 '19 at 12:41
  • @WashingtonGuedes The JVM maintains its own current default time zone. JVMs I have used all pick up the host OS’s time zone only when they launch. If the host OS changes its default time zone, in the JVMs I have used, that has no effect on a running JVM. ASFAIK, this behavior is not in the Java specs, and is left as an implementation detail. Also, JVMs I have used will take as a launch variable a time zone, thereby overriding the use of the host OS zone. Furthermore, any Java code of any app in the JVM can at any time change the JVM’s current default time zone. So I always specify zone in code – Basil Bourque Nov 19 '19 at 16:09