Json 时间段格式应为dd、MMM、yyyy、hh、mm格式
您好,我正在发送Json 时间段格式应为dd、MMM、yyyy、hh、mm格式,json,date,Json,Date,您好,我正在发送JSON数据,JSON必须在后端进行验证。时间段应采用这种格式---dd,MMM,yyyy,hh,mm 这是我的JSON { "equipmentID":"234", "modality":"healthcaare", "facilityID":"manipal", "countryCode":"abc", "isoCode":"1234", "problemType":"234", "problemArea":"priy
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实现验证器{
@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;
}
}
但是我得到一个错误,日期的格式不正确。请帮助我。谢谢。匹配需要一个正则表达式 试试这个
^(([0-9])|([0-2][0-9])|([3][0-1]))\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\-\d{4}$
这将适用于检查该月的MMM
<代码>时间< /代码>不同的参数之一。请考虑日期的数据交换的格式格式。这将是一个更好的选择。
[编辑] 改变这个Validation.rule("TimePeriod", rqdata.getTimePeriod()).required().matches("dd-MMM-yyyy hh-mm").run(result);
到
你的问题是格式应该是-
dd,MMM,yyyy,hh,mm
,但是你的代码检查的是dd-MMM-yyyy-hh-mm
。应该是哪一个?还要注意,MMM
是一个月份的文本名,但国际化程度不好。我的同事会将今天的月份缩写为“Mär”。更喜欢ISO 8601,它是a)标准;b)明确;c)易于排序(格式为“yyyy-MM-dd-hh:MM”或“yyy-MM-ddThh:MM”。)此外,不要忘记时区。我建议将UTC存储在数据库中。当您使用JSON时,您应该与其他人做同样的事情,以RFC 3339格式将日期作为字符串传输(这几乎完全相同,但比ISO 8601严格一点;与8601不同,也需要秒)。我也尝试了这一点,但仍然无法使用字符串regEx=“^([0-9])([0-2][0-9])([3][0-1])\-(1月、2月、3月、4月、5月、6月、7月、8月、9月、10月、11月、12月)\-\d{4}时间段,需要验证规则(“QDATA”)匹配(regEx).run(result);没有,我试过了,但仍然不起作用..还有其他解决方案吗?什么类型的rqdata.getTimePeriod()
?字符串是返回类型匹配(“regEx”)
这需要一个正则表达式,您认为与静态字符串匹配会得到结果吗?您有其他解决方案吗?
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);