How to parse a date string using Timezone in GWT

Has anyone succeeded in parsing a date with a custom timezone in GWT? GWT DateTimeFormat allows you to format dates according to the time zone, but I did not find a method for the opposite operation. So what should I do if I have the line "02: 01: 2011" (format "MM: dd: yyyy"). It may have different results in different time zones.

Another problem occurs when trying to change dates, months, etc. How can I do this based on a custom time zone?

Maybe there is a library that can simplify all these operations?


I made a workaround and add a timezone part for each date line that skips this part. Still looking for a more professional solution.

+4
source share
4 answers

Provide the time zone to the client from the server (for example, include it in the date string) or standardize the time zone on the server so that the client can accept a constant time zone. If you include a time zone with a date string, the code snippet should work below.

I have not 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 represent the time zone, you can change the final variable in the format string (Z). See the docs for more details: http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/i18n/client/DateTimeFormat.html

+5
source

I did the following to parse the date in TimeZone tz. This is 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 analyzes the date in the client’s time zone and then sets it to the desired time zone.

+3
source

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

+1
source
 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 it

-3
source

Source: https://habr.com/ru/post/1341134/


All Articles