Vous êtes sur la page 1sur 2

java

import java.util.Scanner;

public class DateValidator {

public static boolean isValidDate(String date) {

try {

String[] parts = date.split("/");

int day = Integer.parseInt(parts[0]);

int month = Integer.parseInt(parts[1]);

int year = Integer.parseInt(parts[2]);

if (day < 1 || day > 31 || month < 1 || month > 12 || year < 0) {

return false;

} else if (month == 4 || month == 6 || month == 9 || month == 11) {

if (day > 30) {

return false;

} else if (month == 2) {

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {

if (day > 29) {

return false;

} else if (day > 28) {

return false;

return true;

} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {

return false;

}
}

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Entrez une date au format jj/mm/aaaa : ");

String date = scanner.nextLine();

scanner.close();

if (isValidDate(date)) {

System.out.println("La date est valide.");

} else {

System.out.println("La date n'est pas valide.");

Vous aimerez peut-être aussi