【问题标题】:The time period format should be in format dd,MMM,yyyy,hh,mm时间段格式应为 dd,MMM,yyyy,hh,mm 格式
【发布时间】:2016-03-22 08:42:20
【问题描述】:

您好,我正在发送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;
    }
}

但我收到一个错误,指出日期格式不正确。请帮帮我。谢谢

【问题讨论】:

  • 您的问题说格式应该是 - dd,MMM,yyyy,hh,mm,但您的代码会检查 dd-MMM-yyyy hh-mm。应该是哪个?另请注意,MMM 是不能很好地国际化的月份的文本名称。我的同事会将今天的月份缩写为“Mär”。首选 ISO 8601,它是 a) 标准; b) 明确的; c) 轻松排序(格式为“yyyy-MM-dd hh:mm”或“yyyy-MM-ddThh:mm”。)另外,不要忘记时区。我建议将 UTC 存储在数据库中。
  • 当你是 JSON 时,你真的应该像其他人一样做同样的事情,并将日期作为 RFC 3339 格式的字符串传输(这几乎完全相同,但比 ISO 8601 更严格;不像 8601秒也是必需的)。
  • 我也试过了,但还是不行 String regEx = "^(([0-9])|([0-2][0-9])|([3][ 0-1]))\-(一月|二月|三月|四月|五月|六月|七月|八月|九月|十月|十一月|十二月)\-\d{4}$"; Validation.rule("TimePeriod", rqdata.getTimePeriod()).required().matches(regEx).run(result);

标签: json date


【解决方案1】:

匹配需要一个正则表达式。

试试这个

^(([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);

【讨论】:

  • 不,我试过了,但还是不行。。还有其他解决方案吗?
  • rqdata.getTimePeriod() 的类型是什么?
  • 字符串是返回类型
  • matches("regex") 这需要一个正则表达式,你认为匹配静态字符串会给你结果吗?
  • 对于这个问题你有其他解决方案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-15
  • 2020-10-30
  • 2021-07-20
  • 2013-12-10
  • 1970-01-01
  • 2021-12-18
相关资源
最近更新 更多