开发者

SimpleDateFormat not catching invalid 13th month

开发者 https://www.devze.com 2023-03-10 11:01 出处:网络
I am attempting to validate a query parameter for a date. If an invalid date is entered i return an 400 BAD_REQUEST response code. However, my validation is not catching an invalid date of \'201113\'.

I am attempting to validate a query parameter for a date. If an invalid date is entered i return an 400 BAD_REQUEST response code. However, my validation is not catching an invalid date of '201113'. It does however, catch an invalid year such as '000012'. I am using the following code:

SimpleDateFormat df = new SimpleDateFormat("yyyymm"); 
    df.setLenient(false);
    try
    {
        df.parse(endDate);
        df.parse(startDate);
    }
    catch(ParseException e)
    {
        throw new WebApplicationException(Response.Status.BAD_REQUEST);
    }

I know that setting lenient to false sho开发者_如何学Gould have fixed this problem, but it persists. Thanks for your time.


"mm" is for minutes. "MM" is for months... Try "yyyyMM" as your format string instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消