I need to force any time related operations to GMT/UTC, regardless the timezone set on the machine. Any convenient way to so in code?
To clarify, I’m using the DB server time for all operations, but it comes out formatted according to local timezone.
Thanks!
The OP answered this question to change the default timezone for a single instance of a running JVM, set the
user.timezonesystem property:If you need to set specific time zones when retrieving Date/Time/Timestamp objects from a database
ResultSet, use the second form of thegetXXXmethods that takes aCalendarobject:Or, setting the date in a PreparedStatement:
These will ensure that the value stored in the database is consistent when the database column does not keep timezone information.
The
java.util.Dateandjava.sql.Dateclasses store the actual time (milliseconds) in UTC. To format these on output to another timezone, useSimpleDateFormat. You can also associate a timezone with the value using a Calendar object:Usefull Reference
https://docs.oracle.com/javase/9/troubleshoot/time-zone-settings-jre.htm#JSTGD377
https://confluence.atlassian.com/kb/setting-the-timezone-for-the-java-environment-841187402.html