From dacadf1054b81b757ef225d107630c97c4f6fc4f Mon Sep 17 00:00:00 2001 From: LightStyle Date: Thu, 27 Nov 2025 10:21:46 +0700 Subject: [PATCH] first commit --- .gitignore | 29 ++++++++ .idea/.gitignore | 3 + .idea/misc.xml | 6 ++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ Lab_work_2.iml | 11 +++ src/DateValidator.java | 159 +++++++++++++++++++++++++++++++++++++++++ src/Main.java | 26 +++++++ 8 files changed, 248 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 Lab_work_2.iml create mode 100644 src/DateValidator.java create mode 100644 src/Main.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6f29fee --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..37ddda4 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Lab_work_2.iml b/Lab_work_2.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Lab_work_2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/DateValidator.java b/src/DateValidator.java new file mode 100644 index 0000000..9eff2f3 --- /dev/null +++ b/src/DateValidator.java @@ -0,0 +1,159 @@ +import java.util.regex.*; + +public class DateValidator { + private String inputDate; + private boolean isValid; + private String dateFormat; + + public DateValidator() { + this.inputDate = ""; + this.isValid = false; + this.dateFormat = "Неизвестный"; + } + + public DateValidator(String inputDate) { + this.inputDate = inputDate; + this.isValid = validateDate(); + this.dateFormat = detectDateFormat(); + } + + public DateValidator(DateValidator other) { + if (other != null) { + this.inputDate = other.inputDate; + this.isValid = other.isValid; + this.dateFormat = other.dateFormat; + } else { + this.inputDate = ""; + this.isValid = false; + this.dateFormat = "Неизвестный"; + } + } + + public String getInputDate() { + return inputDate; + } + + public boolean isValid() { + return isValid; + } + + public String getDateFormat() { + return dateFormat; + } + + public void setInputDate(String inputDate) { + this.inputDate = inputDate; + this.isValid = validateDate(); + this.dateFormat = detectDateFormat(); + } + + private boolean validateDate() { + try { + if (inputDate == null || inputDate.isEmpty()) { + return false; + } + + if (!matchesDateFormat()) { + return false; + } + + return isDateValid(); + } catch (Exception e) { + System.out.println("Ошибка при проверке даты: " + e.getMessage()); + return false; + } + } + + private String detectDateFormat() { + if (inputDate == null || inputDate.isEmpty()) { + return "Неизвестный"; + } + + String germanRegex = "^(0[1-9]|[12][0-9]|3[01])\\.(0[1-9]|1[0-2])\\.[1-9][0-9]{3}$"; + + String britishRegex = "^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/[1-9][0-9]{3}$"; + + String danishRegex = "^(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[0-2])-[1-9][0-9]{3}$"; + + Pattern germanPattern = Pattern.compile(germanRegex); + Pattern britishPattern = Pattern.compile(britishRegex); + Pattern danishPattern = Pattern.compile(danishRegex); + + if (germanPattern.matcher(inputDate).matches()) { + return "Немецкий/Российский"; + } else if (britishPattern.matcher(inputDate).matches()) { + return "Британский/Французский"; + } else if (danishPattern.matcher(inputDate).matches()) { + return "Датский/Финский"; + } else { + return "Неизвестный"; + } + } + + private boolean matchesDateFormat() { + String germanRegex = "^(0[1-9]|[12][0-9]|3[01])\\.(0[1-9]|1[0-2])\\.[1-9][0-9]{3}$"; + String britishRegex = "^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/[1-9][0-9]{3}$"; + String danishRegex = "^(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[0-2])-[1-9][0-9]{3}$"; + + Pattern germanPattern = Pattern.compile(germanRegex); + Pattern britishPattern = Pattern.compile(britishRegex); + Pattern danishPattern = Pattern.compile(danishRegex); + + return germanPattern.matcher(inputDate).matches() || + britishPattern.matcher(inputDate).matches() || + danishPattern.matcher(inputDate).matches(); + } + + private boolean isDateValid() { + try { + String[] parts; + + if (inputDate.contains(".")) { + parts = inputDate.split("\\."); + } else if (inputDate.contains("/")) { + parts = inputDate.split("/"); + } else if (inputDate.contains("-")) { + parts = inputDate.split("-"); + } else { + return false; + } + + int day = Integer.parseInt(parts[0]); + int month = Integer.parseInt(parts[1]); + int year = Integer.parseInt(parts[2]); + + if (month < 1 || month > 12) { + return false; + } + + 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; + } + } catch (NumberFormatException e) { + System.out.print("Ошибка преобразования чисел: " + e.getMessage() + "\n"); + return false; + } catch (ArrayIndexOutOfBoundsException e) { + System.out.print("Неверный формат даты: " + e.getMessage() + "\n"); + return false; + } + } + + private boolean isLeapYear(int year) { + return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); + } + + public String getResultMessage() { + if (!isValid) { + return "Дата некорректна"; + } + return "Дата корректна. Формат: " + dateFormat; + } +} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..ee7e60a --- /dev/null +++ b/src/Main.java @@ -0,0 +1,26 @@ +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + try { + System.out.print("Введите дату: "); + String input = scanner.nextLine().trim(); + + if (input.isEmpty()) { + System.out.print("Ошибка: пустая строка\n"); + return; + } + + DateValidator validator = new DateValidator(input); + + System.out.print("Сообщение: " + validator.getResultMessage()); + + } catch (Exception e) { + System.out.print("Произошла ошибка: " + e.getMessage() + "\n"); + } finally { + scanner.close(); + } + } +} \ No newline at end of file