可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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!
回答1:
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 ...
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:
Calendar tzCal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); PreparedStatement ps = conn.createPreparedStatement("update ..."); ps.setDate("DateColumn", dateValue, tzCal); // Other assignments ps.executeUpdate();
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(""); //... Date dateValue = rs.getDate("DateColumn"); Calendar calValue = Calendar.getInstance(tz); calValue.setTime(dateValue);
回答2:
Also if you can set JVM timezone this way
System.setProperty("user.timezone", "EST");
or -Duser.timezone=GMT
in the JVM args.
回答3:
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
回答4:
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 { GMT.setTimeZone(TimeZone.getTimeZone("GMT")); SYD.setTimeZone(TimeZone.getTimeZone("Australia/Sydney")); }
then format the date with different timezone.
回答5:
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
回答6:
You could change the timezone using TimeZone.setDefault() - even only temporarily, for some operations.
回答7:
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.
回答8:
Wow. I know this is an ancient thread but all I can say is do not call TimeZone.setDefault() in any user-level code. This always sets the Timezone for the whole JVM and is nearly always a very bad idea. Learn to use the joda.time library or the new DateTime class in Java 8 which is very similar to the joda.time library.
回答9:
If you would like to get GMT time only with intiger: var currentTime = new Date(); var currentYear ='2010' var currentMonth = 10; var currentDay ='30' var currentHours ='20' var currentMinutes ='20' var currentSeconds ='00' var currentMilliseconds ='00'
currentTime.setFullYear(currentYear); currentTime.setMonth((currentMonth-1)); //0is January currentTime.setDate(currentDay); currentTime.setHours(currentHours); currentTime.setMinutes(currentMinutes); currentTime.setSeconds(currentSeconds); currentTime.setMilliseconds(currentMilliseconds); var currentTimezone = currentTime.getTimezoneOffset(); currentTimezone = (currentTimezone/60) * -1; var gmt =""; if (currentTimezone !== 0) { gmt += currentTimezone > 0 ? ' +' : ' '; gmt += currentTimezone; } alert(gmt)