I am coding with Java a function that checks if a date it's ok or not and want to jump up of the method if some condition is executed. I've read a similar question but hasn't the same trouble as it occurs in general and that's not what I want to ask.
public static void validar(String data) {
System.out.println (data);
if (data.indexOf(" ") == -1) {
System.out.println ("No hi ha separació entre data i temps");
System.out.println ("Data incorrecta" + "\n");
return;
}
else if (data.indexOf("-") == -1) {
System.out.println ("La data no conté un guions");
System.out.println ("Data incorrecta" + "\n");
return;
}
else if (data.indexOf(":") == -1) {
System.out.println ("El temps de la data no conté :'s");
System.out.println ("Data incorrecta" + "\n");
return;
}
...
It all works when done like this it:
2023-01-17 17:05:26
Data correcta
2023-01-1717:05:26
No hi ha separació entre data i temps
Data incorrecta
2023/01-17 17.05.26
El temps de la data no conté :'s
Data incorrecta
2023-01-17 17.05.26
El temps de la data no conté :'s
Data incorrecta
Have other conditions to check related to the date (if the date's length is 3 and the same for the time and much more) so I've created some array.
If I create them in the middle of the conditions like:
if (data.indexOf(" ") == -1) {
System.out.println ("No hi ha separació entre data i temps");
System.out.println ("Data incorrecta" + "\n");
return;
}
else if (data.indexOf("-") == -1) {
System.out.println ("La data no conté un guions");
System.out.println ("Data incorrecta" + "\n");
return;
}
else if (data.indexOf(":") == -1) {
System.out.println ("El temps de la data no conté :'s");
System.out.println ("Data incorrecta" + "\n");
return;
}
String[] arrDiaHora = data.split(" ");
String diamesany = arrDiaHora[0];
String horaminutsegon = arrDiaHora[1];
String[] arrDiaMesAny = diamesany.split("-");
String strany = arrDiaMesAny[0];
String strmes = arrDiaMesAny[1];
String strdia = arrDiaMesAny[2];
String[] arrHoraMinutSegon = horaminutsegon.split(":");
String strhora = arrHoraMinutSegon[0];
String strminut = arrHoraMinutSegon[1];
String strsegon = arrHoraMinutSegon[2];
if (arrDiaHora.length != 2) {
System.out.println ("No hi ha dos blocs formats per data i temps");
System.out.println ("Data incorrecta" + "\n");
return;
}
...
Return values after the arrays don't work and get 'return' is unnecessary as the last statement in a 'void' method.
If I create the arrays before checking the conditions, I got an ArrayIndexOutOfBoundsException
Any help ?
Thanks in advance !!
It looks like you are trying to split a string that contains a date. Possibly something like this.
2023-05-11 12:25:32.Rather than trying to reinvent the wheel, check out some of these other options.
Option 1: SimpleDateFormat
Why not use
SimpleDateFormatto do the job for you? This method has been around since the beginning and does the job pretty well.SimpleDateFormatwill attempt to parse the date based on a pattern you define. If it fails to parse, it will throw a checked exception.This way you don't need to worry about all of the complicated logic. Instead, you try to parse the date, and if your parse fails, then the date fails validation.
Option 2: Simple Regex
If you aren't concerned with the numbers actually corresponding to a valid date, and just that they match the format you expect, than you can accomplish this via simple pattern matching.
Option 3: DateTimeFormatter
As user Ole V.V. pointed out, you can also use the updated
DateTimeFormatterto parse the string as well. A word of warning here. TheDateTimeFormatterthrows an unchecked exception, so you should be prepared for this behavior. My example below does not catch the exception. You can catch it here, or higher up the chain if you choose.Option 4: Complex Regex
I don't recommend this, because it's nearly impossible to get a good check due to things like leap years. However, if you want to give it a go, there are plenty of examples floating around.
Option 5: String parsing
This is essentially what you are already doing, but a bit more concise.