4

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.

3

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

5 Comments

The question is how to do it without appending timezone part to my date string.
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.
3

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.

1 Comment

or getFormat("...ss z").parse(dateString+" UT") ?
1

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

1 Comment

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.
-3
public static Date getDateGWT(final String strDate, final int style) {
        Date date = null;
        int useStyle = style;
        if (!validStyle(style)) {
            useStyle = DEFAULT_DATE_STYLE;
        }

        if ((strDate != null) && (strDate.trim().length() > 0)) {
            DateTimeFormat df = getDateFormatGWT(useStyle);
            try {
                date = df.parse(strDate);
            } catch (Exception e) {
                date = df.parse(date.toString());
            }
        }
        return date;
    }

     private static DateTimeFormat getDateTimeFormatGWT(final int style) {
        switch(style) {
        case SHORT:
            return DateTimeFormat.getShortDateTimeFormat();
        case MEDIUM:
            return DateTimeFormat.getMediumDateTimeFormat();
        case LONG:
            return DateTimeFormat.getLongDateTimeFormat();
        case FULL:
            return DateTimeFormat.getFullDateTimeFormat();
        default :
            return DateTimeFormat.getMediumDateTimeFormat();
        }        
   }

Try This

1 Comment

How can it help me with timezones?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.