Validating date in java

13 Feb

Validation of data can take place at different layers in even the simplest of applications, as shown in Developing a Simple Facelets Application.The tag) for numerical data at the presentation layer and for a valid range of numbers at the business layer.Unit testing is very important in these situations because they provide good feedback about the correctness of our regular expressions. You can test your program and reassure that your regular expression meets the rules on your policy about the form of date format. Usually, Simple Date Format‘s parse() method is used for validation. look at the example given below: package com.Test; import Parse Exception: Unparseable date: "2012/12/17" // at If parse() method is able to parse the date, then it is considered that input is valid. Date Format.parse(Date // at com.Test. Test Set Lenient.main(Test Set //Mon Dec 17 GMT 2012 So, clearly setting set Lenient(false); corrects the parsing behavior of Simple Date Format.

* In this scenario, there is no fixed format against which the validation is supposed to be done.The Java programming language distinguishes between null and empty strings.An empty string is a string instance of zero length, whereas a null string has no value at all.Rather regular expressions work character by character. [1-9]› to match 3 followed by 0 or 1, or to match 1 or 2 followed by any digit, or to match an optional 0 followed by 1 to 9. [0-9]$"; Pattern pattern = Pattern.compile(regex); for(String date : dates) Output: 1/1/11 : true 01/01/11 : true 01/01/2011 : true 01/1/2011 : true 1/11/2011 : true 1/11/11 : true 11/1/11 : true List dates = new Array List(); //With leading zeros dates.add("01/01/11"); dates.add("01/01/2011"); //Missing leading zeros dates.add("1/1/11"); dates.add("01/1/2011"); dates.add("1/11/2011"); dates.add("1/11/11"); dates.add("11/1/11"); String regex = "^[0-3][0-9]/[0-3][0-9]/(? Because of this, you have to choose how simple or how accurate you want your regular expression to be.