Thursday, January 13, 2011

Date validator for Hours and Minute - HHMM format

There are generic validators for validating date formats like HHMM.
But while my development was going on I thought to write a validator for HHMM format.

I have to Strings e.g. durationFrom and durationTo.
I'm passing these two strings to my function where I'm validating these values for standard format HHMM.

I have written a generic function as follows


myFunction(){

String isValid = null;

String timeStr1 = 1245;

String timeStr2 = 1255;

isValid = validateTimeForHHMM(timeStr1);

if(isValid != null){

//return the message to GUI.

return isValid;

}else{

isValid = validateTimeForHHMM(timeStr2);

if(isValid != null){

//return the message to GUI.

return isValid;

}

if (Integer.parseInt(timeStr2) < Integer.parseInt(timeStr1)) {

return "Duration From cannot be greater than Duration To";

}

}



private String validateTimeForHHMM(String timeStr){

if (timeStr != "") {

for (int i = 0; i < timeStr.length(); i++) {

// If we find a non-digit character we return false.

if (!Character.isDigit(timeStr.charAt(i)))

return "Duration From or To cannot contains characters";

}



if (timeStr.length() > 4 || timeStr.length() < 4) {

return "Invalid Duration From or To, Please enter values between 0000 to 2359";

} else {

String hours = timeStr.substring(0, 2);

if (Integer.parseInt(hours) > 23) {

return "Invalid Duration From or To, Please enter values between 0000 to 2359";

}

String minutes = timeStr.substring(2, 4);

if (Integer.parseInt(minutes) > 59) {

return "Invalid Duration From or To, Please enter values between 0000 to 2359";

}

}

}

return null;

}

No comments:

SpringBoot: Features: SpringApplication

Below are a few SpringBoot features corresponding to SpringApplication StartUp Logging ·          To add additional logging during startup...