tl;dr
A Java programmer should never rely on the JVM’s current default time zone.
- Always specify a proper time zone name in the format of
Continent/Region
, such as Africa/Casablanca
.
- Always confirm the desired/expected zone with the user, when important.
Proper time zone names
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 2-4 letter abbreviation such as CST
, EST
or IST
as they are not true time zones, not standardized, and not even unique(!). For example, if you use IST
for Ireland Standard Time, you might instead get India Standard Time with surprising results.
ZoneId z = ZoneId.of( "America/Montreal" ) ; // Or "Pacific/Auckland", "Africa/Tunis", etc.
Specify time zone
Never rely on the JVM’s current default time zone. That default value can change.
- As the other Answers explain, most JVMs by default use the host OS’ default time zone in play when the JVM launched.
- Or the script launching the JVM may pass a flag to specify a default time zone at launch.
- And during runtime(!) the JVM‘s current default time zone can be set by any code in any thread of any app within the JVM calling
TimeZone.setDefault
. (By the way, TimeZone
is obsoleted by ZoneId
& ZoneOffset
, except for that setter method which should never be called except in the most extreme situation.)
- And, of course, the user or sysadmin can change their current default time zone on the host OS which is not detected by running JVMs in most implementations as far as I know.
So, for all those reasons, the JVM’s current default time zone is completely out of the hands of the programmer, and even worse, can change dynamically at runtime. Therefore, using current default is not reliable.
Use the JVM’s current default time zone for only the most casual of purposes. For anything that matters, you simply must confirm the intended zone with the user. This is an inconvenient truth often resisted by programmers, but true nonetheless.
Locale
Tip: Ditto for Locale
. The JVM’s current default locale may or may not match that of the host OS, can change at any moment, and is therefore unreliable. Always confirm the desired/expected Locale
with the user.
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
, and java.util.TimeZone
.
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.
EST
orIST
are not true time zones, not standardized, and not even unique(!). Specify a proper time zone name in the format ofContinent/Region
, such asAmerica/Montreal
,Africa/Casablanca
, orPacific/Auckland
. – Claim