I have researched fairly extensively (I've looked at 5 or 6 stackoverflow posts that have often lead me to stare at the official Java regex resource for 30 minutes at a time) but I'm still struggling with this issue. Here is my code for the String matching method:
public String checkDateFormat(String date)
{
if (date.matches("[0-3][0-9](?:-|/)[0-1][0-9](?:-|/)[0-2][0-9][0-9][0-9] [0-2][0-9]:[0-6][0-9]:[0-6][0-9]"))
{
return date;
}
else if (date.matches("[0-1][0-9](?:-|/)[0-3][0-9](?:-|/)[0-2][0-9][0-9][0-9] [0-2][0-9]:[0-6][0-9]:[0-6][0-9]"))
{
return date;
}
else if (date.matches("[0-2][0-9][0-9][0-9](?:-|/)[0-1][0-9](?:-|/)[0-3][0-9] [0-2][0-9]:[0-6][0-9]:[0-6][0-9]"))
{
return date;
}
else if (date.matches("[0-2][0-9][0-9][0-9](?:-|/)[0-3][0-9](?:-|/)[0-1][0-9] [0-2][0-9]:[0-6][0-9]:[0-6][0-9]"))
{
return date;
}
else
{
throw new InvalidParameterException("(The passed argument is invalid date format: [yyyy/dd/MM, yyyy/MM/dd, MM/dd/yyyy, or dd/MM/yyyy])");
}
}
The current setup for checking the -
and /
conditions is one of many I've tried; I've also attempted these: [-/]
[-\\\\/]
[-&&[/]]
[-[/]]
and probably a few more I don't remember.
The program simply needs to be able to accept -
and /
as different separators in the dd/MM/yyyy
format as I'm reading this info from multiple csv files that contain the format both as dd/MM/yyyy
and dd-MM-yyyy
.
EDIT: I've updated my code to this for the time being, but I still fail to pass the data through this validation method. To be more specific about the problem, the specific string I'm checking is 2016-01-01
. I've also tried using the code Ole V.V. provided using the DateTimeFormatter class, but to no success.