My first answer is that you shouldn’t care about the default time zone setting of your JVM. Your Java code shouldn’t rely on it anyway. The default setting can be changed at any time from another part of your program or another program running in the same JVM, so is fragile. Instead specify explicit time zone in your time zone sensitive operations.
If you still want to rely on the default, here’s hack to set it to some continent/city time zone if it happens to be the GMT-05:00
that you consider undesirable.
ZoneId defaultZoneId = ZoneId.systemDefault();
ZoneId desiredTimeZone = null;
if (defaultZoneId.equals(ZoneId.of("GMT-05:00"))) {
// Pick a date in summer
LocalDate dateInSummer = Year.now().atMonthDay(MonthDay.of(Month.JULY, 7));
DateTimeFormatter zoneFormatter = DateTimeFormatter.ofPattern("zzzz", Locale.ENGLISH);
for (String zid : ZoneId.getAvailableZoneIds()) {
ZoneId zone = ZoneId.of(zid);
String timeZoneName = dateInSummer.atStartOfDay(zone).format(zoneFormatter);
if (zid.startsWith("America/") && timeZoneName.equals("Eastern Standard Time")) {
desiredTimeZone = zone;
break;
}
}
}
System.out.println("Time zone: " + desiredTimeZone);
On my JDK 11 I got:
Time zone: America/Panama
To set it as the default we unfortunately have to go through the poorly designed and otherwise long outdated TimeZone
class:
TimeZone.setDefault(TimeZone.getTimeZone(desiredTimeZone));
There is one more reason to warn against using America/Panama or some other time zone ID instead of GMT-05:00: While Panama has been using offset -05:00 from GMT/UTC since some time in 1908, it hasn’t always, so for historic dates you will get incorrect times from using it.
According to timeanddate.com the places that use Eastern Standard Time all year (no summer time/DST) are:
- Nunavut - Southampton Island only (Coral Harbour) (Canada) (America/Coral_Harbour time zone)
- Quintana Roo (Mexico) (America/Cancun time zone)
- Cayman Islands (America/Cayman)
- Jamaica (America/Jamaica)
- Panama (America/Panama)
My Java additionally lists America/Atikokan (also in Canada).
The list doesn’t include any place in the United States. I believe that none of the above have been using offset -05:00
always. That is to say, each of them has had a different offset at some point in history, and Java will use that different offset for historic dates.
So which time zone to use?
Edit: Andreas in a comment pointed out that neither Etc/GMT+5 nor SystemV/EST5 has the problem I mentioned about applying offsets to historic dates that are probably unexpected. So you may consider one of those. The pros of each are, as I see them:
America/Xxx Etc/GMT+5 SystemV/EST5 GMT-05:00
Official IANA time zone? Yes Yes
With slash? Yes Yes Yes
Continent/city? Yes
Good for historical dates? Yes Yes Yes
Prints as EST/Eastern Standard Time? Yes Yes
(Your starting point, GMT-05:00, is only included for the comparison. And which ones are really good for historic dates of course depends on what you require for those.)
Links