TL;DR
- Your formats are (variants of) the same format.
- Use a formatter for validating whether a date string is valid.
- In particular use
DateTimeFormatter
from java.time.
- Don’t use
!=
for comparing strings.
They are variants of the same format
Unless I completely misunderstood, the variation of formats are that the month can be one or two digits and the day of month can be one or two digits. We can handle that as one single format. Like two other answers I believe that for most purposes it’s better to perform a full validation of whether we’ve got a valid date. Not just a validation of the number of digits and the positions of the slashes (which, as the answer by Susannah Potts says, a regex can do). So what we need to do is:
- Define a formatter
- Try to parse; if an exception is thrown, the string wasn’t a valid date string in any of your format variants.
1. Define a formatter
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("M/d/uu")
.withResolverStyle(ResolverStyle.STRICT);
One pattern letter M
will match a month number in either one or two digits. Similar for d
for day of month.
Let’s see this in action first.
System.out.println(LocalDate.parse("12/28/66", dateFormatter));
System.out.println(LocalDate.parse("11/06/37", dateFormatter));
System.out.println(LocalDate.parse("11/2/58", dateFormatter));
System.out.println(LocalDate.parse("03/14/35", dateFormatter));
System.out.println(LocalDate.parse("05/04/68", dateFormatter));
System.out.println(LocalDate.parse("09/3/06", dateFormatter));
System.out.println(LocalDate.parse("5/23/99", dateFormatter));
System.out.println(LocalDate.parse("1/06/00", dateFormatter));
System.out.println(LocalDate.parse("2/8/76", dateFormatter));
Output is:
2066-12-28
2037-11-06
2058-11-02
2035-03-14
2068-05-04
2006-09-03
2099-05-23
2000-01-06
2076-02-08
2.Try to parse and trap for exception
String dateString = "12/28/66";
try {
LocalDate.parse(dateString, dateFormatter);
System.out.println(dateString + " is valid");
} catch (DateTimeParseException dtpe) {
System.out.println(dateString + " is not valid: " + dtpe.getMessage());
}
12/28/66 is valid
Try it with a date string that doesn’t follow any of the formats:
String dateString = "ab/cd/ef";
ab/cd/ef is not valid: Text 'ab/cd/ef' could not be parsed at index 0
java.time
I am recommending java.time, the modern Java date and time API. The classes Date
and SimpleDateFormat
used in one answer are poorly designed and long outdated, the latter in particular notoriously troublesome. java.time is so much nicer to work with.
What went wrong in your code?
There are two problems.
One, as you said, this if
condition always evaluates to true
, which causes your result
to be set to false
:
if((Character.toString(date.charAt(2))!= "/")||(Character.toString(date.charAt(5))!="/"))
Comparing strings with !=
doesn’t work the way you expect. This tests whether the two are the same string object, not whether the strings are unequal. Character.toString()
creates a new string object, so this will always be another object than "/"
. So even though the string from Character.toString()
contains a slash too, !=
will evaluate to true.
Two, you are not validating that the characters before, between and after the slashes are digits.
Links