【问题标题】:how to fix the date regex?如何修复日期正则表达式?
【发布时间】:2020-12-27 20:23:25
【问题描述】:

我正在尝试将正则表达式仅用于 dd/MM/yyyy 日期格式,但如果我输入它接受的 dd/MM/yy 值。我想知道如何让它只接受 dd/MM/yyyy 值。

如果我使用

new SimpleDateFormat("dd/MM/yyyy").parse(value); 

代码继续接受dd/MM/yy 格式。

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());
    }
}

【问题讨论】:

  • 请在这里用英语提问或在Stackoverflow Portugues提问。顺便说一句,您为什么要尝试使用正则表达式重新格式化日期String?您可以使用java.time.LocalDate 和合适的java.time.format.DateTimeFormatters。
  • 我尝试使用但仍然接受值 dd / mm / yyyy
  • 你需要一个DateTimeFormatter.ofPattern("dd/MM/uu").withResolverStyle(ResolverStyle.STRICT);,它只接受两位数的年份。
  • DateTimeFormatter withResolverStyle = DateTimeFormatter.ofPattern("dd/MM/yyyy") .withResolverStyle(ResolverStyle.STRICT); LocalDate.parse(value, withResolverStyle);
  • 一件事:不要用y 表示年份,它表示年代,在年代不明确的情况下可能会造成麻烦。使用u。否则:很高兴它有帮助,不客气。

标签: java regex spring string date


【解决方案1】:

我建议您从过时且容易出错的 java.util 日期时间 API 和 SimpleDateFormat 切换到 modern java.time 日期时间 API 和相应的格式化 API(包,java.time.format)。从 Trail: Date Time 了解有关现代日期时间 API 的更多信息。

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

正则表达式的解释:

  1. \d 指定一个数字
  2. X{n}speciifiesX, exactly n times

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多