/
Skob.m.a
/
WorkSpace
Обзор
Документация
Войти
/
Skob.m.a
/
WorkSpace
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Task_14_5
51 строка
1 KB
Skob.m.a
create Task_14_5
01 дек 2024, 23:27
01 дек 2024, 23:27
3ea8575
Код
Авторство
О чём код?
import java.util.regex.*; public class DateValidator { public static boolean isValidDate(String date) { String regex = "^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/(19|20)\\d\\d$"; // Проверка формата if (!Pattern.matches(regex, date)) { return false; } String[] parts = date.split("/"); int day = Integer.parseInt(parts[0]); int month = Integer.parseInt(parts[1]); int year = Integer.parseInt(parts[2]); if (month == 2) { if (isLeapYear(year)) { return day <= 29; } else { return day <= 28; } } else if (month == 4 || month == 6 || month == 9 || month == 11) { return day <= 30; } else { return day <= 31; } } private static boolean isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } public static void main(String[] args) { String[] testDates = { "29/02/2000", "30/04/2003", "01/01/2003", "29/02/2001", "30-04-2003", "1/1/1899", "31/12/2020", "29/02/2024" }; for (String date : testDates) { System.out.println(date + ": " + (isValidDate(date) ? "Правильная" : "Неправильная")); } } }