Java 如何确定日期正则表达式?
我试图将正则表达式仅用于dd/MM/yyyy日期格式,但如果输入dd/MM/yy值,它会接受。我想知道如何使其仅接受dd/MM/yyyy值 如果我使用Java 如何确定日期正则表达式?,java,regex,spring,string,date,Java,Regex,Spring,String,Date,我试图将正则表达式仅用于dd/MM/yyyy日期格式,但如果输入dd/MM/yy值,它会接受。我想知道如何使其仅接受dd/MM/yyyy值 如果我使用 new SimpleDateFormat("dd/MM/yyyy").parse(value); 代码继续接受dd/MM/yy格式 我建议您从过时且容易出错的java.util日期时间API和SimpleDataFormat切换到java.time日期时间API和相应的格式化API包java.time.format。从了解
new SimpleDateFormat("dd/MM/yyyy").parse(value);
代码继续接受dd/MM/yy格式
我建议您从过时且容易出错的java.util日期时间API和SimpleDataFormat切换到java.time日期时间API和相应的格式化API包java.time.format。从了解有关现代日期时间API的更多信息 示例运行: 正则表达式解决方案: 示例运行: 正则表达式的解释: \d指定一个数字 X{n}X,正好n次
请在这里用英语提问或继续提问。顺便问一下,为什么要用正则表达式重新格式化日期字符串?您可以使用java.time.LocalDate和合适的java.time.format.DateTimeFormatters。我尝试使用但仍然接受值dd/mm/yyyyyy您需要一个DateTimeFormatter.ofPatterndd/mm/uu.WithResolversTyle.STRICT;,只接受两位数的年份。DateTimeFormatter with Resolverstyle=DateTimeFormatter.of模式dd/MM/yyyy。withResolverStyle Resolverstyle.STRICT;LocalDate.parsevalue,带ResolverStyle@德哈尔,谢谢你
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String value = "10/02/20";
Pattern pattern = Pattern.compile("^([0-2][0-9]||3[0-1])/(0[0-9]||1[0-2])/([0-9][0-9])?[0-9][0-9]$");
Matcher matcher = pattern.matcher(value);
System.out.println(matcher.find());
}
}
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Define the format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String dateString;
LocalDate date = null;
boolean valid;
do {
valid = true;
System.out.print("Enter a date in dd/MM/yyyy format: ");
dateString = scanner.nextLine();
try {
// Try to parse the input string
date = LocalDate.parse(dateString, formatter);
} catch (DateTimeParseException e) {
System.out.println("Invalid format");
// Set `valid` to false in case of exception
valid = false;
}
} while (!valid);// Loop as long as valid is false
System.out.println("Date in ISO-8601 format: " + date);
System.out.println("Date in dd/MM/yyyy format: " + date.format(formatter));
}
}
Enter a date in dd/MM/yyyy format: 09/09/20
Invalid format
Enter a date in dd/MM/yyyy format: 09/09/2020
Date in ISO-8601 format: 2020-09-09
Date in dd/MM/yyyy format: 09/09/2020
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String dateString;
boolean valid;
do {
valid = true;
System.out.print("Enter a date in dd/MM/yyyy format: ");
dateString = scanner.nextLine();
if (!dateString.matches("\\d{2}\\/\\d{2}\\/\\d{4}")) {
System.out.println("Invalid format");
valid = false;
}
} while (!valid);// Loop as long as valid is false
System.out.println(dateString);
}
}
Enter a date in dd/MM/yyyy format: 09/09/20
Invalid format
Enter a date in dd/MM/yyyy format: 09/09/2020
09/09/2020