Take the 2-minute tour ×

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.


share|improve this question
possible duplicate of stackoverflow.com/questions/308683/… –  Yuval Adam Apr 13 '10 at 8:23
Actually, his problem is a subset of mine, but I found a solution. –  SyBer Apr 13 '10 at 8:31
add comment

7 Answers

up vote 28 down vote accepted

The OP answered this question to change the default timezone for a single instance of a running JVM, set the user.timezone system property:

java -Duser.timezone=GMT ... <main-class>

If you need to set specific time zones when retrieving Date/Time/Timestamp objects from a database ResultSet, use the second form of the getXXX methods that takes a Calendar object:

Calendar tzCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
ResultSet rs = ...;
while (rs.next()) {
    Date dateValue = rs.getDate("DateColumn", tzCal);
    // Other fields and calculations

Or, setting the date in a PreparedStatement:

PreparedStatement ps = conn.createPreparedStatement("update ...");
ps.setDate("DateColumn", dateValue, tzCal);
// Other assignments

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 and java.sql.Date classes store the actual time (milliseconds) in UTC. To format these on output to another timezone, use SimpleDateFormat. You can also associate a timezone with the value using a Calendar object:

TimeZone tz = TimeZone.getTimeZone("<local-time-zone>");
Date dateValue = rs.getDate("DateColumn");
Calendar calValue = Calendar.getInstance(tz);
share|improve this answer
One thing to note about setting the timezone for the entire JVM is that it affects everything, including such things as logging. Just something to keep in mind if that's the desired effect. –  Herminator Apr 13 '10 at 10:16
Yep, that fine. Just one point to mention, that I actually set the user.timezone directly in code, rather then via the -D parameter. –  SyBer Apr 13 '10 at 21:10
@SyBer: That works but you must ensure that you set this before any method calls the TimeZone.getDefault() method. If not you will have some confusion since the default is cached after the first execution. This also means it could be a problem if you do this inside an API/library (hidden from plain view) - be sure to document heavily what you are doing. –  Kevin Brock Apr 14 '10 at 0:24
add comment

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(..) and Calendar.setTimeZone(..) for details

share|improve this answer
add comment

I had to set the JVM timezone for Windows 2003 Server because it always returned GMT for new Date();


Or your appropriate time zone. Finding a list of time zones proved to be a bit challenging also...

Here are two list;



share|improve this answer
add comment

for me, just quick SimpleDateFormat,

  private static final SimpleDateFormat GMT = new SimpleDateFormat("yyyy-MM-dd");
  private static final SimpleDateFormat SYD = new SimpleDateFormat("yyyy-MM-dd");
  static {

then format the date with different timezone.

share|improve this answer
add comment

You could change the timezone using TimeZone.setDefault() - even only temporarily, for some operations.

share|improve this answer
add comment

Also if you can set JVM timezone this way

System.setProperty("user.timezone", "EST");

or -duser.timezone=GMT in the JVM args.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer


