views:

701

answers:

4

I'm using GWT on the client (browser) and Joda Time on the server. I'd like to perform some DB lookups bounded by the day (i.e. 00:00:00 until 23:59:59) that a request comes in, with the time boundaries based on the user's (i.e. browser) timezone.

So I have the GWT code do a new java.util.Date() to get the time of the request, and send that to the server. Then I use Joda Time like so:

new DateTime(clientDate).toDateMidnight().toDateTime()

The trouble of course is that toDateMidnight(), in the absence of a specified TimeZone, will use the system's (i.e. the server's) TimeZone. I've been trying to find a simple way to pass the TimeZone from the browser to the server without much luck. In GWT I can get the GMT offset with:

DateTimeFormat.getFormat("Z").fmt(new Date())

which results in something like "-0400". But Joda Time's DateTimeZone.forID() wants strings formatted like "America/New_York", or an integer argument of hours and minutes. Of course I can parse "-0400" into -4 hours and 0 minutes, but I'm wondering if there is not a more straightforward way of doing this.

+2  A: 

You could use java.util.Date's getTimezoneOffset() method. It's deprecated, but that's pretty usual for Date handling in GWT currently.

And AFAIR, you can specify something similar to "UTC+4" in Joda time.

Update: I looked it up, and it's "+04:00". Or use DateTimeZone.forOffsetHours() or even forOffsetMillis().

Chris Lercher
Yeah, using java.util.Date.getTimezoneOffset() is certainly better than string parsing. It's deprecated, but apparently still implemented in GWT. At least it works correctly in my browser for EDT.
Caffeine Coma
Don't worry about using deprecated Date methods in GWT too much - just have a look at `com.google.gwt.user.datepicker.client.CalendarUtil`... I wouldn't do this outside of GWT though, and I hope, that GWT will improve in this respect soon!
Chris Lercher
Actually, something quite odd- Date.getTimezoneOffset() returns (positive) 240 for me. Since I am in Cleveland (EDT), I expect this to be -240 (i.e. I am 4 hours behind GMT). And this is in Java unit tests, not even GWT.
Caffeine Coma
Please avoid "Etc/" format - just use +04:00 - http://joda-time.sourceforge.net/api-release/org/joda/time/DateTimeZone.html#forID%28java.lang.String%29
JodaStephen
@JodaStephen: Thanks, good to know! I updated my answer.
Chris Lercher
A: 

otherwise, there are other ways to get timezone offset with + & -.

import java.util.TimeZone;

use: TimeZone.getDefault().getRawOffset()

this function will return the offset time in millisecond about your phone seeting. For Example, GMT-04:00 is equals to (-4)*60*60*1000 = -14400000.

After some operations to get the number which you want.

NickLai
Unfortunately, `java.util.TimeZone` isn't available in GWT. But it may be possible to use `com.google.gwt.i18n.client.TimeZone.getOffset(Date)`, if you already have such a TimeZone object.
Chris Lercher
+1  A: 

Gwittir (http://www.gwtsite.com) is a library for GWT that includes many cool utilities, like databinding, animation, reflection, and more. However, there are some other interesting goodies as well like the new Joda Time integration. If you have ever been frustrated by GWT’s lack of java.util.Calendar support, you’ll love this, as it makes it easy to do date manipulations in your applications.

nevermind
A: 

I have a similar but slightly different problem I think.

I actually need to store the clients timezone on the server, so that I can send out messages about dates stored in their calendar.

The dates are stored in UTC time in google app engine and of course I can store the current Timezone offset when creating the appointment. The problem comes when for instance I want to send out a summary email with a list of upcoming appointments in it. These appointments need to be offset with the correct Timezone adjustments for the client (Im happy to assume that they are still in the same timezone as when they created the appointment).

The real problem comes with Daylight Savings adjustments, so for instance I might have appointments stored for Saturday 30th October 2010 at 1pm (BST[GMT+60]) and Monday 1st November 2010 at 1pm (GMT).

So as you can imagine, I cant just use the current timezone offset (BST) as that would mean that the appointment on Monday 1st November would be listed as 2pm rather than 1pm (GMT+60)

It occurs to me that the best way to deal with this is just to store the timezone offset with each appointment individually, but I feel it would be much better to be able to determine the original timezone correctly in the first place, then just let java do the correct adjustments.

johnvdenley
Just found this really good blog/code which deals at least partially with this issue however it still doesnt deal with the date of the DST switchover http://www.onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/
johnvdenley