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!
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
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
Hope below code will help you.
DateFormat formatDate = new SimpleDateFormat(ISO_DATE_TIME_PATTERN);
formatDate.setTimeZone(TimeZone.getTimeZone("UTC"));
formatDate.parse(dateString);
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)
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.
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.
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.
Use system property:
-Duser.timezone=UTC
String sql = "SELECT CURRENT_TIMESTAMP ;
…
OffsetDateTime odt = myResultSet.getObject( 1 , OffsetDateTime.class ) ;
I recommend you write all your code to explicitly state the desired/expected time zone. You need not depend on the JVM’s current default time zone. And be aware that the JVM’s current default time zone can change at any moment during runtime, with any code in any thread of any app calling TimeZone.setDefault
. Such a call affects all apps within that JVM immediately.
Some of the other Answers were correct, but are now outmoded. The terrible date-time classes bundled with the earliest versions of Java were flawed, written by people who did not understand the complexities and subtleties of date-time handling.
The legacy date-time classes have been supplanted by the java.time classes defined in JSR 310.
To represent a time zone, use ZoneId
. To represent an offset-from-UTC, use ZoneOffset
. An offset is merely a number of hour-minutes-seconds ahead or behind the prime meridian. A time zone is much more. A time zone is a history of the past, present, and future changes to the offset used by the people of a particular region.
I need to force any time related operations to GMT/UTC
For an offset of zero hours-minutes-seconds, use the constant ZoneOffset.UTC
.
Instant
To capture the current moment in UTC, use an Instant
. This class represent a moment in UTC, always in UTC by definition.
Instant instant = Instant.now() ; // Capture current moment in UTC.
ZonedDateTime
To see that same moment through the wall-clock time used by the people of a particular region, adjust into a time zone. Same moment, same point on the timeline, different wall-clock time.
ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
You mention a database.
To retrieve a moment from the database, your column should be of a data type akin to the SQL-standard TIMESTAMP WITH TIME ZONE
. Retrieve an object rather than a string. In JDBC 4.2 and later, we can exchange java.time objects with the database. The OffsetDateTime
is required by JDBC, while Instant
& ZonedDateTime
are optional.
OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
In most databases and drivers, I would guess that you will get the moment as seen in UTC. But if not, you can adjust in either of two ways:
Instant
: odt.toInstant()
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
Also if you can set JVM timezone this way
System.setProperty("user.timezone", "EST");
or -Duser.timezone=GMT
in the JVM args.
Execute this line in MySQL to reset your timezone:
SET @@global.time_zone = '+00:00';
You could change the timezone using TimeZone.setDefault():
TimeZone.setDefault(TimeZone.getTimeZone("GMT"))
Source: Stackoverflow.com