Regex for Java Date validation - java

Am checking validation of date using regex how it works for date but not for Year ? .Please help me to solve this issue.
Output :
false
true
false
true
Expected Output :
false
false
false
false
public static void main(String args[])
{
System.out.println(dateFormatValidate("ddMMyyyy","^(0?[1-9]|[12][0-9]|3[01])(0?[1-9]|1[012])((19|20)\\d\\d)?", "08s21988"));
System.out.println(dateFormatValidate("ddMMyyyy","^(0?[1-9]|[12][0-9]|3[01])(0?[1-9]|1[012])((19|20)\\d\\d)?", "08021s88"));
System.out.println(dateFormatValidate("ddMMyyyy","^(0?[1-9]|[12][0-9]|3[01])(0?[1-9]|1[012])((19|20)\\d\\d)?", "s8021988"));
System.out.println(dateFormatValidate("ddMMyyyy","^(0?[1-9]|[12][0-9]|3[01])(0?[1-9]|1[012])((19|20)\\d\\d)?", "0802198s"));
}
public static boolean dateFormatValidate(String format, String regex, String value) {
try {
if (value != null && !"".equals(value.trim()) && format != null && !"".equals(format.trim()))
{
if ((regex != null && !"".equals(regex.trim()) && Pattern.matches(regex, value)) || regex != null || "".equals(regex))
{
SimpleDateFormat dformat = new SimpleDateFormat(format);
dformat.setLenient(false);
dformat.parse(value);
return true;
} else
return false;
} else
return false;
} catch (Exception e) {
return false;
}
}

#sunleo I don't think so it has anything to do with your regex, as I have just tried your pattern on these four dates you provide and it doesn't capture any of them.
I would say the error is in this if:
if ((regex != null && !"".equals(regex.trim()) && Pattern.matches(regex, value)) || regex != null || "".equals(regex))
{
// your code
}
In the cases you provided in your main:
regex != null - all cases true
!"".equals(regex.trim()) - all cases true
Pattern.matches(regex, value)) - all cases false
regex != null - all cases true
"".equals(regex)) - all cases false
Which gives us following if:
if ( (true AND true AND false) OR true OR false )
which is the same as:
if ( false OR true OR false )
which gives in all cases:
true
So why you still managed to get two false outputs? Probably an exception was thrown here:
SimpleDateFormat dformat = new SimpleDateFormat(format);
dformat.setLenient(false);
dformat.parse(value);
So in your catch statement change return false to e.printStackTrace();.
Also my recommendation would be to rearrange this particular if first then check it for the cases that should be false (in this example all of them).
How?
First rearrange if then I would start with debugging and checking the if components to see their values and if they are computed correctly.
Also I think the regex is not correct at all (even if it's not the cause of the wrong output), if you always use ddMMyyyy format and only 19xx/20xx years, try this pattern:
^(0[1-9]|[1-2][0-9]|3[0-1])(0[1-9]|1[0-2])(19|20)\d{2}$
NOTE
I have not checked any of this (except regex) via any IDE as I do not have any here.

Related

Why is the code ignoring one of the IF requirements?

I'm sure it's obvious but can't figure out what I'm missing. Code is:
} else if (flag != null && date != null &&
!date.equals("") && (disability != null || flagEnd == null))
//do stuff
}
I've double checked the variables. flag is not null, date IS null, disability is null and flagEng is null. The code shouldn't be stepping into this because the date is null, and yet it is. Do I have this written incorrectly?
Note: date is a string. flagEnd is a java.util.Date. Thanks!
Edit: Here is the code that creates the date variable.
String date = (rs.getString("date") != null && rs.getString("date").length() > 0 ? rs.getString("date") : "NULL");
Instead of date != null, should I maybe instead have this?
!date.equals("NULL")
The string was actually storing the word "NULL", as opposed to being an empty string. Once I changed the code to !date.equals("NULL"), it skipped the ELSE IF statement like it should have.

JAVA Comparing two Strings isn't working

So here's a snippet of code I'm working on:
String direction = s.readLine();
System.out.println(direction);
if (direction.equals("up") != true && direction.equals("down") != true &&
direction.equals("left") != true && direction.equals("right") &&
direction.equals(null) != true) {
System.out.println("Invalid Solution file");
System.exit(0);
}
What it is supposed to do is read a line from a text file (using a BufferedReader) and then if the line isn't either a valid direction or blank then it should print "Invalid Solution" and exit.
The problem is that no matter what the direction string is the if statement still runs. I put in a println to check whether the direction was being read correctly but it seems absolutely fine. So why isn't the code working as intended?
Part of your problem is readability. Fix that and your problem is 90% solved:
private static List<String> DIRECTIONS = Arrays.asList("up", "down", "left", "right");
then
if (!DIRECTIONS.contains(direction)) {
System.out.println("Invalid Solution file");
System.exit(0);
}
The other 10% was how to check for null, which is direction == null, but if you use this code you don't need to, because contains(null) will conveniently return false.
You code is much more complex than it is needs to.
Consider this instead:
Set<String> validDirections = new HashSet<>(Arrays.asList("up", "down", ...
if (validDirections.contain(direction.toLowerCase()) {
// good ...
} else {
// bad ..
}
You can make validDirections a global constant for example; so it could be used in other places as well.
What I am trying to explain here is: your code is low-level. Low level code is hard to write, read, maintain and extend. Programming is always about creating good abstractions. Or vice versa: if you don't use abstractions, you end up with pretty abstract code, like the one you are showing here!
For example: if you need another direction, you have to put into your already way too complicated if condition. In my solution, you just put it into the statement that builds that Set.
Finally: your error message, is saying nothing. So, that string is bad; but why is it? Wouldn't it be better to at least print the string that caused the error?!
Here && direction.equals("right") I think you have done a mistake since it is on contradiction with the rest :
direction.equals("up") != true &&
direction.equals("down") != true &&
direction.equals("left") != true
You test the negation in the most of conditions but direction.equals("right") tests the affirmation.
Try it , it's the same thing but less verbose and more readable :
if (direction !=null && !direction.equals("up") &&
!direction.equals("down") &&
!direction.equals("left") &&
!direction.equals("right") ){
System.out.println("Invalid Solution file");
System.exit(0);
}
First, you should not use != true with a boolean statement, it is bad form. Rewrite like this:
direction !=null &&
!direction.equals("up") &&
!direction.equals("down") &&
!direction.equals("left") &&
!direction.equals("right")
Your error was that you did not include the != true part on one of your statements within the compound if. Replace with the above code to solve the issue.
I'm confused why you are using !=true when your .equals method already returns a boolean. Try this.
String direction = s.readLine();
System.out.println(direction);
if ( direction!=null && !direction.equals("up") && !direction.equals("down")&& !direction.equals("left")&& direction.equals("right")){
System.out.println("Invalid Solution file");
System.exit(0);
}
Try the following code:
boolean match = false;
if (direction.equals("up"))
{ match = true; }
if (direction.equals("down"))
{ match = true; }
if (direction.equals("left"))
{ match = true; }
if (direction.equals("right"))
{ match = true; }
if (direction.equals(null))
{ match = true; }
if (match == false){
System.out.println("Invalid Solution file");
System.exit(0);
}
You might also want to trim the direction string after reading from file.
The quals method returns a boolean so the result does not need to be compared with the true or false value. Also, I would start with null comparison - boolean expressions in Java are shortened so if this part will be fulfilled rest of the expression is not evaluated. The correct expression might look like this:
 
if (direction == null || (!direction.equals("up") && !direction.equals("down") && !direction.equals("left") && !direction.equals ("right "))) {
}
But this code is not readable. You could use enums or list of Strings like below
List<String> directions = Arrays.asList("up", "down", "left", "right");
String direction = "readValue"
if (!directions.contains(direction)) {
System.out.println("Invalid direction");
System.exit(0)
}

Why is Matcher.group() throwing illegal state exception?

Why does the below code work fine
Matcher reg = Pattern.compile("(A|B)\\w{2}(C|D)").matcher("");
while ((line=reader.readLine()) != null)
{
if (!loading || reg.reset(line).matches())
{
if (reg.reset(line).matches()) {
String id = reg.group(1);
}
}
}
but
while ((line=reader.readLine()) != null)
{
if (!loading || reg.reset(line).matches())
{
String id = reg.group(1);
}
}
throws IllegalSyntaxException?
I was surprised because I am already calling matches in the if condition. The expectation is that it returns the string matching the group, throws the exception instead.
java.lang.IllegalStateException: No match found
What am I missing?
If loading == false, reg.reset(line).matches() won't be executed, because !loading already is true. In your first example you then "again" check if there is a match and only then try to get the group. In your second example, you just assume that there is a match because you got there, which may not be true.
If the code you posted is all you do in this if-statement, you may get rid of the !loading check, as it doesn't matter if it's true or false - as soon as you find a match, you'll execute the code within the body, if you cannot find a match, it won't be executed.

Can't get java to match regex with matches()

I have a problem where I'm trying to get a parameter to match the format 'Uddd' where 'U' MUST be the letter U and 'ddd' can be any 3 digits from 0-9.
My current code:
//borrow method
public boolean borrow(String borrowerID)
{
//if the borrower ID matches the format 'Uddd'
if (borrowerID.matches("U([0-9]{3})"))
{
//if the status is available
if (status == 'A')
{
this.borrowerID = borrowerID;
this.status = 'O';
this.dateBorrowed = currentDate;
return true;
}
//is not available
else
{
return false;
}
}
//does not match format
else
{
return false;
}
}
For some reason it's not validating properly. When I tried inputting '1' as the parameter, it still returned true.
Is there something I'm missing?
It should not be possible for that method to return true if the input is "1". I can only suggest you ensure you are passing in "1" and that the method is the one being called.
That can be done with a simple debug statement at the top, something like:
System.out.println ("Calling correct function with [" + borrowerId + "]");
at the start of the function.
I'd also suggest a bit of clean-up to make the function easier to code and read, along the lines of:
// borrow method
public boolean borrow(String borrowerID)
{
// Temp debug statement.
// System.out.println ("borrow [" + borrowerId + "]");
// "Not available" or "invalid borrower" means reject request.
if (status != 'A')
return false;
if (! borrowerID.matches("U([0-9]{3})"))
return false;
// Okay to borrow.
this.borrowerID = borrowerID;
this.status = 'O';
this.dateBorrowed = currentDate;
return true;
}
This is a lot cleaner than all those return-else-do-something constructs, and it follows a "fail fast" paradigm.
Some people tend to dislike multiple return points but that's usually because they don't understand why they're considered bad (spaghetti code). With a short function like this, it doesn't pose a problem.
I get good results with your regex : "1" -> false, "UU123" -> false, "U1234" -> false, "U132" -> true.
but you can use \d instead of [0-9] :
borrowerID.matches("U(\\d{3})")
actually that's not possible.when input 1 it won't return true but it return false.may be your status is equal 'A' that should be reason to return true
String borrowerID="1";
boolean matches = borrowerID.matches("U([0-9]{3})");
System.out.println(matches);
output>>
false
if you only want to find is regex match or not then use this .or put a sout and check does status value.definitely it should be A
if (borrowerID.matches("U([0-9]{3})")) {
return true;
} else {
return false;
}
There is no issues with your regex
System.out.println("U888".matches("U([0-9]{3})"));
Output true
System.out.println("1".matches("U([0-9]{3})"));
Output false
Try debug your code, with few breakpoint inside your borrow function
You can also optimize your like
if (borrowerID.matches("U([0-9]{3})") && (status == 'A')) {
this.borrowerID = borrowerID;
this.status = 'O';
this.dateBorrowed = currentDate;
return true;
} else {
return false;
}
I don't understand onething, you are checking the status againist 'A' and inside assignin '0', does it make sense?(Anyway it's upto you)

Finding if a specific character exists at a specific index

so I'm trying to figure out how to create a condition for my decision structure. I'm making a program that converts military time to conventional time. When times are entered, they must be in a XX:XX format, where X equals a digit. I'm wondering, how can I make a validation that checks to make sure that ":" always exists as a colon and is in the same spot?
myString.charAt(5) == ':'
Just change 5 to whatever you need, and check string length before you do this, so you don't ask for something past the end of a short string.
You could search the string with indexOf(), which returns the index at which the character is found. This will check if a colon is in the string and if it's in the right position.
if("10:00".indexOf(':') == 2)
// do something
It will return -1 if the value is not found. Check out the java documentation for more information.
Here is the answer for it
Suppose you had string that contains XX:XX
yourString="XX:XX"
yourString.contains(":") - return true - if exists
":" takes the 3rd position. So it will be 2(0,1,2)
yourString.charAt(2) == ':'
Together , it will be
if(yourString.contains(":") && yourString.charAt(2) == ':'){
//your logic here
}else{
something here
}
Here is one approach to that,
String[] test = new String[] { "00:00", "NN:NN", "12,13", "12:13" };
for (String fmt : test) {
boolean isValid = true;
for (int i = 0; i < fmt.length(); i++) {
char c = fmt.charAt(i);
if (i == 2) {
if (c != ':') { // here is the colon check.
isValid = false;
break;
}
} else {
// This checks for digits!
if (!Character.isDigit(c)) {
isValid = false;
break;
}
}
}
System.out.println(fmt + " is " + isValid);
}
Output is
00:00 is true
NN:NN is false
12,13 is false
12:13 is true
#WillBro posted a good answer but I give you another option:
myString.indexOf(":") == 5
With indexOf you can also look for full String inside a String.
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
But for string format validations I suggest you to use Regular Expresions.
Here's a full example that does what you want to do:
http://www.mkyong.com/regular-expressions/how-to-validate-time-in-24-hours-format-with-regular-expression/

Categories

Resources