简体   繁体   中英

DateTimeFormat date validation error

I am trying to validate a date using GWT. For most purposes, using the code below works correctly:

boolean valid = true;
    try{
        DateTimeFormat.getFormat(DATE_FORMAT).parseStrict(value);
    } catch (Exception e) {
        valid = false;
    }
return valid;

For example :

10/35/2014 15/25/2014 104/15/2014 10/500/2014

all return an error.

The problem I am having occurs with this instance:

  • 10/20/20144

The DateTimeObject recognizes 20144 as a valid year. However, when I insert this into the SQL datatbase, I receive an error. Is there anyway to alter DateTimeFormat so that this year also throws an error? Or any type of work around??

Any help is appreciated!

EDIT:

I was able to solve this issue two ways.

  1. splitting the original string and checking length of year is only 4 characters

  2. creating a date 12/31/3999 and checking that the parsed date is before this date.

Thanks

20144 is a valid year. It won't happen soon, but it will. The easiest solution in your case is to set a limit on the maximum year that you allow. It can be 9999 (the maximum year in Microsoft SQL ), or something closer depending on your project requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM