时间段格式应采用格式dd,MMM,yyyy,hh,mm

时间:2016-03-22 08:42:20

标签: json date

您好我发送JSON数据,JSON必须在后端验证。时间段应采用此格式--- dd,MMM,yyyy,hh,mm

这是我的JSON

{
    "equipmentID":"234",
    "modality":"healthcaare",
    "facilityID":"manipal",
    "countryCode":"abc",
    "isoCode":"1234",
     "problemType":"234",
    "problemArea":"priyanka",
    "equipmentStatus":"sdsd",
    "name":"taneja",
    "phoneNumber":"13333344",
    "extension":"12123",
    "description":"x ray machine error",
    "shortDescription":"2",
     "timePeriod":"03-12-2011 04-37",
     "serviceCode":"sdfdf",
     "locale":"werfd",
    "requestingApp":"icenter", 
    "examNumber":"sdd", 
    "seriesNumber":"dfdf", 
    "imageNumber":"dfdfd"
}

这是验证类     公共类RequestValidator实现Validator {

    @Override
    public ValidationResult validate(String objectName, RequestData rqdata) {
        // TODO Auto-generated method stub

        ValidationResult result = new ValidationResult();

        if (rqdata == null) {
            result.addError("error.invalidObjectGraph", "Object graph not initialized correctly");
            return result;
        }
        Validation.rule("EquipmentId", rqdata.getEquipmentID()).required().run(result);
        Validation.rule("Modality", rqdata.getModality()).required().run(result);
        Validation.rule("FacilityID", rqdata.getFacilityID()).required().run(result);
        Validation.rule("CountryCode", rqdata.getCountryCode()).required().maxLength(3).matches("^[a-zA-Z]*$")
                .run(result);
        Validation.rule("ProblemType", rqdata.getProblemType()).required().run(result);
        Validation.rule("Name", rqdata.getName()).required().maxLength(20).run(result);
        Validation.rule("PhoneNumber", rqdata.getPhoneNumber()).required().maxLength(25).matches("[0-9]+").run(result);
        Validation.rule("Extension", rqdata.getExtension()).required().maxLength(10).matches("[0-9]+").run(result);
        Validation.rule("Description", rqdata.getDescription()).required().maxLength(300).run(result);
        Validation.rule("ShortDescription", rqdata.getShortDescription()).required().maxLength(80).run(result);
        Validation.rule("TimePeriod", rqdata.getTimePeriod()).required().matches("dd-MMM-yyyy hh-mm").run(result);
        Validation.rule("Locale", rqdata.getLocale()).required().run(result);

        System.out.println("value of requesting app is:" + rqdata.getRequestingApp());
        Validation.rule("RequestingApp", rqdata.getRequestingApp()).required().matches("icenter").run(result);
        System.out.println(result.getErrorDetails());
        return result;
    }
}

但是我收到的错误是日期格式不合适。请帮帮我。感谢

1 个答案:

答案 0 :(得分:0)

匹配需要在其中使用正则表达式。

试试这个

^(([0-9])|([0-2][0-9])|([3][0-1]))\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\-\d{4}$

这适合检查本月的MMM

Time其中一个参数有不同的方法。请考虑日期数据交换的纪元格式。这将是一个更好的选择。

[编辑]

更改此

Validation.rule("TimePeriod", rqdata.getTimePeriod()).required().matches("dd-MMM-yyyy hh-mm").run(result);

String regEx = "^(([0-9])|([0-2][0-9])|([3][0-1]))\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\-\d{4}$";
Validation.rule("TimePeriod", rqdata.getTimePeriod()).required().matches(regEx).run(result);