0

I'm kinda new to GWT.

I have been trying to parse a date in the format "dd-MMM-yyyy HH:mm z" but I get an exception when the timezone is EDT or BST.

Doesn't GWT support these timezones while parsing? If so, is there any alternative way which I can use to parse the date using EDT?

Please help.

Code snippet:

DateTimeFormat dateParser = DateTimeFormat.getFormat("dd-MMM-yyyy HH:mm z");
String fomattedDate = dateParser.format(date,Timezone.createTimeZone(TimeZoneConstants.americaNewYork));
Date newDate = dateTimeParser.parse(formattedDate);

This line gives me exception.

After reading the docs for DateTimeFormat it said that it supports a lower number of timezones while parsing.

walen
  • 7,103
  • 2
  • 37
  • 58
Prakhar
  • 92
  • 12
  • 1
    Can you share the code you are using? Have you checked if the calls you are making are passing these timezones to the browser, and that it is JS (and not the GWT framework) which is failing to parse them? – Colin Alworth Apr 04 '17 at 04:15
  • Hi Colin I have attached the code snippet. Please have a look and share your inputs. – Prakhar Apr 04 '17 at 06:27
  • It works if you change the format to `dd-MMM-yyyy HH:mm Z` - capital `Z` as time zone. – Adam Apr 04 '17 at 13:16
  • I tried but still givese an illegalArgumentException. I have tried with z, Z, v but it still cant parse the timezone – Prakhar Apr 04 '17 at 13:47
  • Maybe comment is not enough. I've posted my code in an answer. – Adam Apr 04 '17 at 14:02

1 Answers1

2

This code works for me:

TimeZoneConstants timeZoneConstants = GWT.create(TimeZoneConstants.class);
DateTimeFormat dateParser = DateTimeFormat.getFormat("dd-MMM-yyyy HH:mm Z");
String formattedDate = dateParser.format(date, TimeZone.createTimeZone(timeZoneConstants.americaNewYork()));
Date newDate = dateParser.parse(formattedDate);

I have created TimeZoneConstants via deferred binding and changed the format to dd-MMM-yyyy HH:mm Z (capital Z) - with z I did got an IllegalArgumentException indeed.

If you still get issues, try inspect deeply the parsing function to track down the exact problem.

Adam
  • 5,403
  • 6
  • 31
  • 38
  • Thanks Adam. Just one more question here, when I put the value after parsing in a java.util.Date object it agains converts my formatted date to GMT timezone. How can I keep it in EDT timezone and put in a date object ? Actually I need to set values in this object once its parsed. – Prakhar Apr 04 '17 at 17:51
  • I'm afraid it's hardly possible in GWT since java.util.Date has always the browser's timezone. Check [Client side time zone support in GWT](http://stackoverflow.com/questions/1686448/client-side-time-zone-support-in-gwt) question for example or ask another question. I'm glad I could help with parsing. – Adam Apr 04 '17 at 19:25