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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
public class EssaiDate {
/**
* @param args
* @throws IOException,
* ParseException, java.text.ParseException
* @throws ParseException
*/
public static void main(String[] args) throws IOException, ParseException,
java.text.ParseException {
// TODO Auto-generated method stub
boolean check_format = true;
Date date = new Date();
String str_format;
do{
Afficher("Entrer une date - (dd-MM-yyy)");
Scanner sc = new Scanner(System.in);
str_format = sc.nextLine();
check_format = test_format(str_format);
}
while(!check_format);
DateFormat df = new SimpleDateFormat("dd-MM-yyyy", Locale.FRENCH);
df.setLenient(false);
date = df.parse(str_format);
String format = DateFormat.getDateInstance().format(date);
Afficher("Format US: " + date);
Afficher("Format FR: " + format);
}
public static boolean test_format(String str){
boolean good = false;
Pattern datePattern;
Matcher matcher;
String jj;
String mm;
String yyyy;
datePattern = Pattern.compile("^(\\d{2})-(\\d{2})-(\\d{4})$");
matcher = datePattern.matcher(str.trim());
if(!matcher.find())
{
Afficher("Format date invalide");
good = false;
}
else {
good = true;
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))
{
Afficher("Mauvais Date");
good = false;
}
} return good;
}
private static <T> void Afficher(T e) {
System.out.println(e.toString());
// TODO Auto-generated method stub
}
} |