Java program for reading in a date and validating it with proper calendar regex including leap year and months
I have almost finished my java school assignment and am now going for the bonus task which is to (when I read in a date from the user) I must validate it not only in the correct format (DD-MM-YYY with the dashes inbetween) but I have to validate it according to a proper calendar including leap years and some months only having 30 days instead of others having 31. I have figured out a way of doing this and I will call the isLeapYear() function but what I must first to is break this:
20-10-2009 into individual day month year int values.
NOW the only problem that I am going to have is "cutting" the string that I have into a day month year and validating that. What I mean is if I get a string from the user (and they type in 20-10-2009 ) how could I possibly break this up into the appropriate day month year so in turn I could get that year and check if it is a leap year?
Unfortunately I HAVE to read in a string otherwise it is a "spec violation" for my assignment.
Could anybody point me in the right direction so that I may figure out how to accomplish this?
Re: Reading in a date and validating it with proper calendar regex
This is what I ended up doing...
String VALID_DATE_REGEX = "(0[1-9]|[0-9]|3)[-](0[1-9]|1)[-](20\\d\\d)";
Pattern DATE_PATTERN = Pattern.compile(VALID_DATE_REGEX);
String actDate = "20-12-2020";
Matcher mDateMatch = DATE_PATTERN.matcher( actDate );