Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle calendar TimeZones using Java?

I have a Timestamp value that comes from my application. The user can be in any given local TimeZone.

Since this date is used for a WebService that assumes the time given is always in GMT, I have a need to convert the user's parameter from say (EST) to (GMT). Here's the kicker: The user is oblivious to his TZ. He enters the creation date that he wants to send to the WS, so what I need is:

User enters: 5/1/2008 6:12 PM (EST)
The parameter to the WS needs to be: 5/1/2008 6:12 PM (GMT)

I know TimeStamps are always supposed to be in GMT by default, but when sending the parameter, even though I created my Calendar from the TS (which is supposed to be in GMT), the hours are always off unless the user is in GMT. What am I missing?

Timestamp issuedDate = (Timestamp) getACPValue(inputs_, "issuedDate"); Calendar issueDate = convertTimestampToJavaCalendar(issuedDate); ... private static java.util.Calendar convertTimestampToJavaCalendar(Timestamp ts_) {   java.util.Calendar cal = java.util.Calendar.getInstance(       GMT_TIMEZONE, EN_US_LOCALE);   cal.setTimeInMillis(ts_.getTime());   return cal; } 

With the previous Code, this is what I get as a result (Short Format for easy reading):

[May 1, 2008 11:12 PM]

like image 203
Jorge Valois Avatar asked Oct 23 '08 15:10

Jorge Valois


People also ask

How does Java handle different time zones?

If you cannot change the OS or the JVM timezone, you can still convert a Java Date/Time or Timestamp to a specific time zone using the following two JDBC methods: PreparedStatement#setTimestamp(int parameterIndex, Timestamp x, Calendar cal) – to convert the timestamp that goes to the database.

Does Java Calendar have TimeZone?

Calendar setTimeZone() Method in Java with ExamplesThe setTimeZone(TimeZone time_zone) method in Calendar class takes a Time Zone value as a parameter and modifies or set the timezone represented by this Calendar.

How do I set the time zone in Java?

You can explicitly set a default time zone on the command line by using the Java system property called user. timezone . This bypasses the settings in the Windows operating system and can be a workaround.

How does Java calculate TimeZone?

There are three ways to obtain a TimeZone instance: static TimeZone getDefault(): Gets the default TimeZone of the Java virtual machine. static TimeZone getTimeZone(String ID): Gets the TimeZone for the given ID. static TimeZone getTimeZone(ZoneId zoneId): Gets the TimeZone for the given zoneId.


1 Answers

public static Calendar convertToGmt(Calendar cal) {      Date date = cal.getTime();     TimeZone tz = cal.getTimeZone();      log.debug("input calendar has date [" + date + "]");      //Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT      long msFromEpochGmt = date.getTime();      //gives you the current offset in ms from GMT at the current date     int offsetFromUTC = tz.getOffset(msFromEpochGmt);     log.debug("offset is " + offsetFromUTC);      //create a new calendar in GMT timezone, set to this date and add the offset     Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));     gmtCal.setTime(date);     gmtCal.add(Calendar.MILLISECOND, offsetFromUTC);      log.debug("Created GMT cal with date [" + gmtCal.getTime() + "]");      return gmtCal; } 

Here's the output if I pass the current time ("12:09:05 EDT" from Calendar.getInstance()) in:

DEBUG - input calendar has date [Thu Oct 23 12:09:05 EDT 2008]
DEBUG - offset is -14400000
DEBUG - Created GMT cal with date [Thu Oct 23 08:09:05 EDT 2008]

12:09:05 GMT is 8:09:05 EDT.

The confusing part here is that Calendar.getTime() returns you a Date in your current timezone, and also that there is no method to modify the timezone of a calendar and have the underlying date rolled also. Depending on what type of parameter your web service takes, your may just want to have the WS deal in terms of milliseconds from epoch.

like image 176
matt b Avatar answered Sep 25 '22 10:09

matt b