2

I want to check if a given string is in a particular dateformat. If not user should be able to throw error message.

DateTimeFormat format=DateTimeFormat.getFormat("yyyy-MM-dd");               
Date d= format.parse("1990-10-");

with this input , it should give error. I tried with try and catch , but its not throwing any exception.

  • Ofcourse, there's no way to know if for example `2013-08-12` is in the format `yyyy-MM-dd` (12 August 2013) or `yyyy-dd-MM` (8 December 2013). – Jesper Aug 12 '13 at 07:29

2 Answers2

2

Use java.text.SimpleDateFormat, it throws ParseException.

SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");               
try {
  Date d= format.parse("1990-10-");
} catch (ParseException e) {
 ...
}
  • 1
    No,GWT won't support it. – Suresh Atta Aug 12 '13 at 06:16
  • 1
    yup, GWT client side does not support it –  Aug 12 '13 at 06:18
  • I never worked with GWT, but really? It's standard Java class for crying out loud. Now I know, why some of my colegues bitch about GWT so much. – Radek Wroblewski Aug 12 '13 at 06:19
  • Please have a look [GWT wont convert all java classes] (http://stackoverflow.com/questions/15528500/java-timer-in-gwt/15528569#15528569) – Suresh Atta Aug 12 '13 at 06:21
  • @RadekWroblewski I understood.Your surprise revealed there :) Anyway +1 for try catch block. Good day. – Suresh Atta Aug 12 '13 at 06:38
  • 1
    That's not GWT's point to implement the whole JVM in Javascript. Thus only a limited part of the Java standard libraries is available in the client. – Thibault D. Aug 12 '13 at 19:15
  • 1
    And, no offence, but maybe you shouldn't answer GWT related questions if you have never worked with it :) – Thibault D. Aug 12 '13 at 19:17
  • 1
    For classes in `java.text` to be handled correctly, there are megabytes of properties files that would need to be compiled in - in many cases it isn't possible to test if they will actually be used or not because they are parsed and used at runtime. – Colin Alworth Aug 13 '13 at 13:25