How can I do a date validation with leap years in Java? Unfortunately my program was already working 100% but they told me that I can't use regular expressions, calendar or try-catch. I will explain it step-by-step!
This is my main component (where I implement most things):
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;
}
and in another component called utility (I put the methods that will be used to be able to call them in the implementation (IMPL), all this to carry an order. My utility code is as follows:
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;
}
}
So my problem which I am presenting, is to see another possible solution that can solve my problem. Currently in my component, as you will see in the code above, I am using a method which is handled by a try-catch and for obvious reasons I cannot use it (although it already works 100%)
The functionality of this programming is that I put the data separately in a LOCAL ENVIRONMENT (online), where I put a date.
For example I am putting the date 02/29/2012:
Input:
<com:simpleFieldType>
<com:name>date</com:name>
<com:value>02/29/2012</com:value>
</com:simpleFieldType>
Output:
<ns3:name>flag</ns3:name>
<ns3:value>1</ns3:value>
Then the data you enter does exist and is correct.
But if I put a date 02/30/2012:
Input:
<com:simpleFieldType>
<com:name>date</com:name>
<com:value>02/30/2012</com:value>
</com:simpleFieldType>
Output:
<ns2:adviceCode>100005</ns2:adviceCode>
<ns2:adviceDescription>THE ERROR REPORTED BY THE APPLICATION DOES NOT EXIST: MGBD100005</ns2:adviceDescription>
So that means that it is an incorrect data because it does not exist (leap year date) and at that moment it only throws an error called 10005 (necessary)
The method that you suggest has to be connected with the implement that is:
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());
If you have doubts, I invite you to see the complete code that is in the upper part.
PD: THE PROGRAM ALREADY WORKS 100% FOR ME but... I'm looking for another solution in which it helps me deliver (without using expressions, calendar or try-catch).
PD2: Excuse me if I can't upload all the programming, but there are about 80 components, but the most important thing is only the utility and the Implementation (so that it works).