How to parse date string using Timezone in GWT

10,820

Solution 1

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);

Depending on how you are representing the timezone, you can change the final variable in the format string (the Z). See the docs for details: http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/i18n/client/DateTimeFormat.html

Solution 2

I did the following to parse a date in the TimeZone tz. It's probably dodgy, but it works: -

final long MILLIS_IN_MINUTE = 60000;

Date localDate = DateTimeFormat.getFormat("dd MMM yyyy HH:mm:ss").parse(dateString);

int localOffset = localDate.getTimezoneOffset() * MILLIS_IN_MINUTE;
int targetOffset = tz.getOffset(localDate) * MILLIS_IN_MINUTE;

// Subtract the offset to make this into a UTC date.
return new Date(localDate.getTime() - localOffset + targetOffset);

It parses the date in the client timezone and then adjusts it to the required timezone.

Solution 3

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

Share:
10,820
Zalivaka
Author by

Zalivaka

Updated on August 21, 2022

Comments

  • Zalivaka
    Zalivaka over 1 year

    Has anybody succeeded parsing date string with a custom timezone in GWT? GWT's DateTimeFormat allows to format dates based on time zone, but I haven't found any method for doing opposite operation. So what should I do if I have following string "02:01:2011" (format "MM:dd:yyyy"). It can have different results in different timezones.

    The other problem appears when trying to change dates, months and etc. How can I do it based on a custom timezone?

    Maybe there is any library which can simplify all these operations?


    I have made workaround and add timezone part to each date string which miss that part. Still looking for a more professional solution.

  • Zalivaka
    Zalivaka about 13 years
    The question is how to do it without appending timezone part to my date string.
  • Zalivaka
    Zalivaka about 13 years
    How can it help me with timezones?
  • Zalivaka
    Zalivaka about 13 years
    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.
  • Brad
    Brad about 13 years
    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.
  • Zalivaka
    Zalivaka about 13 years
    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.
  • Brad
    Brad about 13 years
    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.
  • Peter Jamieson
    Peter Jamieson over 11 years
    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.
  • Kieveli
    Kieveli over 9 years
    or getFormat("...ss z").parse(dateString+" UT") ?