tester la validité d'une date en java
bonjour,
comment tester la validité d'une date avec java
Merci
Solution avec les RegEx et pattern
Sinon tu peux faire ca avec les expression regulieres et ces deux imports
Code:
1 2 3
|
import java.util.regex.Matcher;
import java.util.regex.Pattern; |
apres tu verifies dans ta fonction validate de ton Formulaire comme suit:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
public ActionErrors validate( ActionMapping m, HttpServletRequest r) {
ActionErrors errors = new ActionErrors();
Pattern datePattern;
Matcher matcher;
String jj;
String mm;
String yyyy;
datePattern = Pattern.compile("^(\\d{2})/(\\d{2})/(\\d{4})$");
matcher = datePattern.matcher(Date.trim());
if(!matcher.find()) {
errors.add("Date", new ActionMessage("errortype"));
// ici donc traitement qd il n'a pas trouvé
}
if(matcher.find()) {
// on teste les champs precisement
jj = matcher.group(1);
mm = matcher.group(2);
yyyy = matcher.group(3);
if( Integer.valueOf(jj) <0 || Integer.valueOf(jj)>31
|| Integer.valueOf(mm) <0 || Integer.valueOf(mm)>12
|| Integer.valueOf(yyyy) <1970)
{
{
errors.add("Date", new ActionMessage("errortype"));
// ici donc traitement qd il n'a a trouvé un bon format mais
// les informations dd/mm/yyyy ne sont pas bonne
}
}
return errors;
} |
Voila moi j'utilise cela et ca marche tres bien.
Dis moi si tu comprends pas tout.
Mat