You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java不使用try-catch/正则/Calendar实现含闰年判断的日期校验

Java环境下兼容闰年判断的无违规依赖日期合法性校验实现

需求背景

  • 现有Java项目日期校验功能已可正常运行,但当前实现不符合编码规范要求:禁止使用正则表达式、Calendar类、try-catch异常捕获机制
  • 需替换公共工具类中日期校验方法的内部逻辑,完全兼容上层业务调用,无需修改现有业务分支代码,同时正确支持闰年日期判断
  • 项目共包含约80个业务组件,本次仅需调整核心工具类逻辑即可,无需改动其他业务代码

现有代码说明

核心业务组件逻辑

主逻辑类中已预留日期校验调用位置,现有上层判断分支不可修改,代码如下:

private static final Logger LOGGER = LoggerFactory.getLogger(MGBDRVDC.class);
@Override
public Long executeValAccount(Map<String, Object> parameterIn) {
    Long flag = null;
    Utility util = new Utility();
    if(util.validateAcc((String) parameterIn.get(Constants.ACCOUNTNUMBER.getValue()))){
        LOGGER.info("Valid Account");

/// IMPLEMENTATION (Here commanded to call the method for Date Validation ).

if(parameterIn.get("date") != null
                && Utility.isDateStringValid((String) parameterIn.get(Constants.DATE.getValue()), "MM/dd/uuuu")
                || (util.dateNull((String)parameterIn.get("date"))) ) {
        }else{
            this.addAdvice(Constants.MGBD100005.getValue());

     ////////////////////////////////////////////////

}else {
    this.addAdvice(Constants.MGBD100005.getValue());
    LOGGER.info("Invalid Account");
}

    return flag;
}

现有工具类实现

公共工具类Utility存放通用方法供业务层调用,当前日期校验方法通过try-catch捕获解析异常判断合法性,不符合编码约束,代码如下:

public class Utility {

public boolean validateAcc(String accountNumber){
    return accountNumber!=null && accountNumber !="" && accountNumber.matches(Constants.MATCHES.getValue());
}

//method I currently use to implement date validation
public static boolean isDateStringValid(String dateTimeString, String desiredFormat) {
    boolean result = true;
    java.time.format.DateTimeFormatter formatter =
            java.time.format.DateTimeFormatter.ofPattern(desiredFormat)
                    .withResolverStyle(java.time.format.ResolverStyle.STRICT);
    try {
        if ((dateTimeString.contains(":") && !desiredFormat.contains(":")) ||
                (!dateTimeString.contains(":") && desiredFormat.contains(":"))){
            return false;
        }
        else if (dateTimeString.contains(":") && desiredFormat.contains(":")) {
            java.time.LocalDateTime localDate = java.time.LocalDateTime.parse(dateTimeString, formatter);
        }
        else {
            java.time.LocalDate localDate = java.time.LocalDate.parse(dateTimeString, formatter);
        }
    } catch ( java.time.format.DateTimeParseException dtpe) {
        result = false;
    }
    return result;
}
}

校验规则

  • 合法日期场景:传入02/29/2012这类闰年合法2月29日时,判定为合法数据,返回flag值为1
    合法日期请求报文示例:
    <com:simpleFieldType>
    <com:name>date</com:name>
    <com:value>02/29/2012</com:value>
    </com:simpleFieldType>
    
    合法返回示例:
    <ns3:name>flag</ns3:name>
    <ns3:value>1</ns3:value>
    
  • 非法日期场景:传入02/30/2012这类不存在的公历日期时,判定为非法数据,返回错误码MGBD100005
    非法日期请求报文示例:
    <com:simpleFieldType>
    <com:name>date</com:name>
    <com:value>02/30/2012</com:value>
    </com:simpleFieldType>
    
    错误返回示例:
    <ns2:adviceCode>100005</ns2:adviceCode>
    <ns2:adviceDescription>THE ERROR REPORTED BY THE APPLICATION DOES NOT EXIST: MGBD100005</ns2:adviceDescription>
    

适配要求

  • 新实现的isDateStringValid方法必须完全兼容现有调用逻辑,无需修改上层业务判断分支即可直接接入,原有不可修改的分支逻辑如下:
if(parameterIn.get("date") != null
                && Utility.isDateStringValid((String) parameterIn.get(Constants.DATE.getValue()), "MM/dd/uuuu")
                || (util.dateNull((String)parameterIn.get("date"))) ) {
        }else{
            this.addAdvice(Constants.MGBD100005.getValue());
  • 实现逻辑严格遵守编码约束:禁止使用正则表达式、禁止使用Calendar类、禁止使用try-catch异常捕获机制

内容的提问来源于stack exchange,提问作者Carlos fernando

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 06:30:49