我如何做一个日期验证(LeapYear)与我们的异常方法(Try-catch)?



如何在Java中使用闰年进行日期验证?不幸的是,我的程序已经工作100%,但他们告诉我,我不能使用正则表达式,日历或try-catch。我将一步一步地解释!

这是我的主要组件(我在这里实现了大多数东西):

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的组件(我把将要使用的方法放在能够调用它们的实现(IMPL)中)中,所有这些都要进行排序。我的实用程序代码如下:

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;
}

}

所以我要提出的问题是,找到另一种可能的解决方案来解决我的问题。目前,在我的组件中,正如您将在上面的代码中看到的,我正在使用一个由try-catch处理的方法,由于明显的原因,我不能使用它(尽管它已经100%工作)

这个程序的功能是,我把数据单独放在一个LOCAL ENVIRONMENT (online)中,在这个LOCAL ENVIRONMENT中我放了一个日期。

例如,我写日期02/29/2012:

输入:

<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:

输入:

<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>

所以这意味着它是一个不正确的数据,因为它不存在(闰年日期),此时它只抛出一个名为10005(必要)的错误

您建议的方法必须与实现相连接,即:

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

如果您有疑问,我建议您查看上面部分的完整代码。

PD:这个程序对我来说已经100%有效了,但是……我正在寻找另一个解决方案,它可以帮助我交付(不使用表达式,日历或try-catch)。

PD2:对不起,如果我不能上传所有的编程,但有大约80个组件,但最重要的是只有实用程序和实现(使其工作)。

我会这样做:

  1. split("/")。如果你没有得到三个元素,日期是无效的。
  2. 将每个元素转换为int(您必须编写自己的代码以避免NumberFormatException)
  3. 检查年份是否为闰年,然后检查月份和日期的取值范围是否有效。

相关内容

最新更新