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!
for me, just quick SimpleDateFormat,
then format the date with different timezone.
I had to set the JVM timezone for Windows 2003 Server because it always returned GMT for new Date();
-Duser.timezone=America/Los_Angeles
Or your appropriate time zone. Finding a list of time zones proved to be a bit challenging also...
Here are two list;
http://wrapper.tanukisoftware.com/doc/english/prop-timezone.html
http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=%2Frzatz%2F51%2Fadmin%2Freftz.htm
You could change the timezone using TimeZone.setDefault():
create a pair of client / server, so that after the execution, client server sends the correct time and date. Then, the client asks the server pm GMT and the server sends back the answer right.
I would retrieve the time from the DB in a raw form (long timestamp or java's Date), and then use SimpleDateFormat to format it, or Calendar to manipulate it. In both cases you should set the timezone of the objects before using it.
See
SimpleDateFormat.setTimeZone(..)
andCalendar.setTimeZone(..)
for detailsThe OP answered this question to change the default timezone for a single instance of a running JVM, set the
user.timezone
system property:If you need to set specific time zones when retrieving Date/Time/Timestamp objects from a database
ResultSet
, use the second form of thegetXXX
methods that takes aCalendar
object: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.Date
andjava.sql.Date
classes 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