Rectangle 27 2

The solution discussed there is to add config to the appengine maven plugin.

<jvm_flag>
                    <jvm_flag>-Dappengine.user.timezone=UTC</jvm_flag>
                </jvm_flag>

Passing the argume as -D does not work (nor adding .impl) as discuess in older solutions.

java - Timezone Error when running GWT/GAE app - Stack Overflow

java google-app-engine gwt
Rectangle 27 1

TimeZone.setDefault(TimeZone.getTimeZone("GMT-05:00"));

for example this is for Bogota, Lima, Quito .. you can choose your Zone here

java - Timezone Error when running GWT/GAE app - Stack Overflow

java google-app-engine gwt
Rectangle 27 0

TimeZone.setDefault(TimeZone.getTimeZone("GMT-05:00"));

for example this is for Bogota, Lima, Quito .. you can choose your Zone here

java - Timezone Error when running GWT/GAE app - Stack Overflow

java google-app-engine gwt
Rectangle 27 0

In my experience, the following best practice significantly reduces complexity and confusion when dealing with dates and timezones in gwt:

  • Whenever operating/storing dates within the application, treat all dates as milliseconds since epoch in GMT timezone. You can store them as string or int, it doesn't really make a difference.
  • Whenever displaying the date to the end user, format the date using appropriate timezone.

For your case, when you create a date on the Server (timezone A) convert it to milliseconds since epoch in GMT before sending it to the Client. On the client, use DateTimeFormat (or write your own date formatter util) to convert it into either timezone B or timezone C as appropriate.

I missed the section on handling time zones in the DateTimeFormat java doc. Seems it provides exactly the kind of fuctionality I need! I will use it in combination with the approach you propose. Thanks!

java - Client side time zone support in GWT - Stack Overflow

java gwt timezone
Rectangle 27 0

You can't change the GWT timezone, hence all java.util.Date's has the browser timezone. You will need to handle the current timezone setting manually.

I would prefer option 3. At least until GWT RPC maybe someday will support for overriding the CustomFieldSerializer's

java - Client side time zone support in GWT - Stack Overflow

java gwt timezone
Rectangle 27 0

are all the same. As per that I infer the relation between time and timezone as the amount of time subtracted from GMT or added to the GMT. So 16.00 GMT becomes 1400 GMT -0200 or 1900 GMT +0300. Keeping that in mind we have to work the other way around to get the desired result.

java - GWT DateTimeFormat reverses timezone value - Stack Overflow

java datetime gwt date timezone
Rectangle 27 0

Either give the timezone to the client from the server (e.g., include it in the date string) or standardize the timezone on the server so that the client can assume a constant timezone. If you include the timezone with the date string, the below code snippet should work.

I havent tested this, but according to the docs, it should work:

String dateStr = "04/21/2011 01:37:36 -0800;
DateTimeFormat format = new DateTimeFormat("MM/dd/yyyy HH:mm:ss Z");
Date date = format.parse(dateStr);

Adding the timezone to the string is the right way to handle this IMO. Another way is to manage offsets yourself, but then you have to worry about what timezone you are in anyway AND know the correct offset (and this could change because of DST). Typically, I always keep my dates in UTC and then let DateTimeFormat put them in the correct timezone when I format them for output to the browser. You can pass a timezone into the format method along with the UTC Date.

But you can't retrieve date like 04/14/2011 from UI and parse it into java Date based on the custom timezone. I like 'Bartek Jablonski' solution but now I haven't got time to investigate it.

Where is the custom timezone coming from? If you have it as a GWT timezone object, you can just add the offset to the date (timezone.getOffset() returns the number of minutes from UTC). Dates will always be parsed as UTC and the timezone will always have the correct offset from UTC.

it would be nice if format and parse were reflective. There should be a parse method that takes a timezone and it should also work with the v and z formats that format outputs.

java - How to parse date string using Timezone in GWT - Stack Overflow

java javascript gwt date timezone
Rectangle 27 0

You should do a bit of study on how tracking date-time on computers works. Start with this question, Daylight saving time and time zone best practices. Do some other searching on StackOverflow to find many helpful discussions and code examples.

Basically your question is a duplicate of many others.

Your question does not give enough information to help us help you. Exactly how does your GWT app capture the date-time from the user, in exactly what data type? From exactly where does the server store the date-time value and using exactly what data type?

Example code in Joda-Time 2.5.

DateTimeZone zoneParis = DateTimeZone.forID( "Europe/Paris" );
DateTimeZone zoneKolkata = DateTimeZone.forID( "Asia/Kolkata" );

DateTime nowUtc = DateTime.now( DateTimeZone.UTC );
DateTime nowParis = now.withZone( zoneParis );
DateTime nowKolkata = now.withZone( zoneKolkata );

Joda-Time and java.time both parse and generate strings in ISO 8601 format by default.

String output = nowUtc.toString();
java.util.Date date = newUtc.toDate();

@AndreiVolgin (A) The author did not really identify the source and handling of the data, as noted in the top of my answer. (B) The question carries a java tag. (C) There are ways to communicate Java values including Joda-Time/java.time with GWT, such as the Vaadin framework.

I think it's important to consider all relevant tags when offering an answer.

java - issue with timezone while saving date through screen in gwt - S...

java gwt
Rectangle 27 0

I use the following method:

TimeZone.createTimeZone(String tzJSON)

Applications can get such a string from the TimeZoneConstants class, or it can request the string from the server. Either way, the application obtains the original string from the data provided in the TimeZoneConstant.properties file, which was carefully prepared from CLDR and Olson time zone database.

You typically do not want all of these Strings on your client. I use an RPC call to retrieve only the string that I need.

Note that this is a more accurate method for creating a time zone, because time zone offset may be different on different dates.

java - get TimeZone in gwt - Stack Overflow

java gwt
Rectangle 27 0

I'm assuming you are using RPC calls for server-client communication here. Also assuming that you don't care about timezone B, and you know what timezone C is on the server.

You have a few options here:

  • Calculate the desired date in the server (no Java limits on what you can do there) and send it in a String to be displayed to the client, so you don't have to do anymore transformations on the client.
  • Calculate the offset between timezone A and C on the server, apply it to all the Date objects you are passing to the client and just display them on the client.

if for some reason none of these were valid for you

  • Calculate the offset, send it to the client and apply it to any Date you receive from the server by transforming to ms, adding the offset and then creating a Date object again.

java - Client side time zone support in GWT - Stack Overflow

java gwt timezone
Rectangle 27 0

Simply format the date in UTC format at client side and pass the date as string to the server and store the data in UTC format in database as well.

On the server side everything would be fine, since formatting the date would use the timezone of the server, which is what the date is stored in. On the client side, however, GWT will use the timezone of the client machine, and so there could be a discrepancy.

DateTimeFormat f = DateTimeFormat.getFormat("MMM dd yyyy");
TimeZoneConstants t = (TimeZoneConstants) GWT.create(TimeZoneConstants.class)
TimeZone est = TimeZone.createTimeZone(t.americaNewYork());
int offset = est.isDaylightTime(date) ? +240 : +300;
TimeZone tz = TimeZone.createTimeZone(offset);
String date = f.format(user.getBirthDate(), est);

There are a few other possible solutions, but one of these two might do the trick.

So should I follow solution 2? I mean is it better idea to store all dates in after converting UTC format?

Yes store the dates in UTC format in database to make it consistent.

How to get client's timezone on server side for conversion? Let my app locale is en_US but my timezone is IST. when I try to convert date with this code it convert date in UTC format and it is not correct.

Simply convert it into UTC format at client side and then pass the formatted value to the server.

java - GWT timezone issue - Stack Overflow

java gwt
Rectangle 27 0

Doing a GWT.create on the TimeZoneConstants class is bad game according to the GWT Javadoc [1]. So what I did was create a class on the server-side that parses /com/google/gwt/i18n/client/constants/TimeZoneConstants.properties and builds a cache of all the JSON objects for each time zone (indexed by their Olson TZID).

My site is running on jboss, so I copied TimeZoneConstants.properties into my site's war/WEB-INF/lib directory (probably didn't need to copy it there since the GWT jars are already there). Then I have a singleton class that does the parsing when constructed:

InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(PROPERTIES_FILE);
InputStreamReader isr = new InputStreamReader(inStream);
BufferedReader br = new BufferedReader(isr); 
for (String s; (s = br.readLine()) != null;) { 
  // using a regex to grab the id to use as a key to the hashmap
  // a full json parser here would be overkill
  Pattern pattern = Pattern.compile("^[A-Za-z]+ = (.*\"id\": \"([A-Za-z_/]+)\".*)$");
  Matcher matcher = pattern.matcher(s);     
  if (matcher.matches()) {
    String id = matcher.group(2);
    String json = matcher.group(1);

    if (!jsonMap.containsKey(id)) {
      jsonMap.put(id, json);
    }
  }
} 
br.close();
isr.close();
inStream.close();

Finally I make an RPC call to get the TimeZoneInfoJSON to the client (assuming the server knows which TimeZoneID I'm interested in):

getTimeZone(new PortalAsyncCallback<String>() {
  public void onSuccess(String tzJson) {
    timeZone = TimeZone.createTimeZone(TimeZoneInfo.buildTimeZoneData(tzJson));
  }
});

Not the most elegant solution but it has given me a way to display dates and times for a specific time zone over DST transitions.

java - Convert Olson Time Zone ID to TimeZoneConstant in GWT (Client S...

java gwt timezone
Rectangle 27 0

If you time zone is fixed, why would you use a string to represent date/time? You can send a standard Java Date object to the client. If you want, you can even store all dates and times as Longs and pass Longs only. You also send the GWT's TimeZone Json string for your time zone (once per session). You can find it in the GWT - there is a file with strings for all time zones.

On a client you use DateTimeFormat with many predefined formats to display whatever you need: full date, month and date, date and time, etc. Just remember to create TimeZone object from this Json string and use it in DateTimeFormat.getFormat(...).format(Date, TimeZone).

With this approach you don't have to worry about DST changes (they are encoded in that Json string) and locales. You only pass simple Date or Long objects.

Andrei, it's a little too late to change to Date or Long. All our DTOs passed to client are String and have all info in ISO8601. The exception being a day which is yyyy-MM-dd format. Thinking on this, it may be that that is a problem b/c when DateTImeFormat.parse is called it parses using client's TZ not "market time" as I mentioned earlier. I guess I'm missing something here.

Also, if you look closely at test, we map Date (hour) to hour labels. 01 is 1:00am. 24 is midnight of next day. This is not such a simple effort when you take into account transition days.

You can specify a time zone in your string (in GMT+- format). Parser will take it into account when creating a Date object.

I might try that. However, doesn't the offset from GMT differ when day is a transition day, e.g., Nov-04-2012 and Mar-10-2013.

Also, you can create your custom parser since you know (and control) the format of the string, but appending a time zone to your strings may be enough to make a standard GWT parser usable for your use case.

GWT Date Handing... have client respect server's timezone - Stack Over...

gwt date timezone
Rectangle 27 0

My favorite approach is to save dates as Long values adjusted to midnight in GMT:

Long dateInMills = new Date().getTime();
dateInMills -= dateInMills % (24 * 60 * 60 * 1000));

// to show this date you can use, for example
DateTimeFormat.getFormat(PredefinedFormat.DATE_MEDIUM).format(new Date(dateInMills));

When you show this date in GWT, it will use the default time zone of a browser to display it.

If you want to have more control over it, for example to allow a user with a one time zone in a browser to see dates with a different time zone, you will have to pass the desired TimeZone object to DateTimeFormat#format method.

While many people do this (track time as count from epoch), I have to disagree. Makes debugging and sanity-checking difficult if not impossible. Problematic values are no longer obvious. Can be confused as different tools/environments use different epochs and different count resolution (whole seconds, milliseconds, microseconds, and nanoseconds). Java libraries, JDBC drivers, and databases are all built to handle the nitty-gritty detail of tracking the date-time, so let them. Do you perform bit-level arithmetic? Work with text by octets? We have classes and libraries to do that work for us.

Every problem can be over-engineered if you have enough time on your hands. For most web-based apps, however, storing dates as Long values and displaying them using standard GWT DateTimeFormat object works perfectly fine.

java - issue with timezone while saving date through screen in gwt - S...

java gwt
Rectangle 27 0

Recently I passed upon this project: gwt-calendar-class which emulates Calendar and TimeZone in javascript.

It looks nice, but I have already done it by appending timezone part to my data string. I don't know if your solution is mature enough that is why I can't mark it correct. When I have time I will try to test it.

java - How to parse date string using Timezone in GWT - Stack Overflow

java javascript gwt date timezone
Rectangle 27 0

I have a similar issue with v2.4.

However, I found I could get the Tokyo zone by using the browser's default setting via:

Date d = new Date();
TimeZone timezone = TimeZone.createTimeZone(d.getTimezoneOffset());

Offset changes depending on a date. You may get the wrong time zone as a result.

java - Sending a date and timezone from GAE server to GWT client - Sta...

java google-app-engine gwt timezone gwt-rpc
Rectangle 27 0

SimpleDateFormat doesn't ignore the timezone, as it has adjusted the time, parsing 15:34:35 +0500 into 17:34:35 NOVST (where NOVST is +0700, the local timezone), it's just that java.util.Date is only a timestamp from Epoch and has no notion of a timezone (see the javadoc: everything is relative to the local timezone).

If you want to get the timezone of the client on the server-side, you'll have to parse the timezone field out of the serialized date (you could split on spaces, keep the last part the timezone part and pass it to java.util.TimeZone.getTimeZone() prefixed with GMT). Alternatively, you can use JodaTime whose DateTime type keeps track of the timezone info; something like:

DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss Z");

DateTime dt = fmt.parseDateTime(dateHeader);
System.out.println("x = " + fmt.print(dt));
System.out.println("Timezone = " + dt.getZone().toTimeZone().getDisplayName());

thanks for your comment. I was thinking about those two options, separate the timezone, or use JodaTime. I am already trying JodaTime, and it works well on hosted mode, but I can't seem to make it work on a tomcat instance.

java - GWT client - server SimpleDateFormat changes timezone to defaul...

java simpledateformat date-parsing
Rectangle 27 0

I ended up storing the number of seconds since epoch instead of an actual date value. This way, mySQL doesn't try to fix the timezone.

java - TimeZones in Google Web Toolkit (GWT), GAE & Google Cloud SQL (...

java mysql google-app-engine gwt timezone
Rectangle 27 0

You can't change the GWT timezone, hence all java.util.Date's has the browser timezone. You will need to handle the current timezone setting manually.

I would prefer option 3. At least until GWT RPC maybe someday will support for overriding the CustomFieldSerializer's

a java.util.Date object has no notion of timzone.

java - Client side time zone support in GWT - Stack Overflow

java gwt timezone