first commit
This commit is contained in:
29
.gitignore
vendored
Normal file
29
.gitignore
vendored
Normal file
@@ -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
|
||||
3
.idea/.gitignore
generated
vendored
Normal file
3
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
6
.idea/misc.xml
generated
Normal file
6
.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/Lab_work_2.iml" filepath="$PROJECT_DIR$/Lab_work_2.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
11
Lab_work_2.iml
Normal file
11
Lab_work_2.iml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
159
src/DateValidator.java
Normal file
159
src/DateValidator.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
26
src/Main.java
Normal file
26
src/Main.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user