I have an instance of Java which seems to be using a completely incorrect time zone. Instead of using the Australia/Sydney time zone which Windows is using, it is using the
Try in your app to get default timezone, or set timezone manually (commented line).
Little example of mine:
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) {
Locale locale = Locale.getDefault();
TimeZone localTimeZone = TimeZone.getDefault();
//TimeZone localTimeZone = TimeZone.getTimeZone("Australia/Sydney");
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale);
dateFormat.setTimeZone(localTimeZone);
Date rightNow = new Date();
System.out.println(locale.toString() + ": " + dateFormat.format(rightNow));
}
}
A Java programmer should never rely on the JVM’s current default time zone.
Continent/Region
, such as Africa/Casablanca
.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.
Never rely on the JVM’s current default time zone. That default value can change.
TimeZone
is obsoleted by ZoneId
& ZoneOffset
, except for that setter method which should never be called except in the most extreme situation.) 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.
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.
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.
Ensure you set the timezone for the JVM when starting the application:
-Duser.timezone="Australia/Sydney"