regex date validation for yyyyMMdd

后端 未结 3 1934
隐瞒了意图╮
隐瞒了意图╮ 2021-01-15 05:32

I\'m looking for a regex string that will validate dates formatted as yyyyMMdd (no delimiters) and works for leap years. The closest thing I found so far only validates dat

3条回答
  •  渐次进展
    2021-01-15 06:20

    I suggest using the java.text.DateFormat as shown in this page :

    public static boolean isValidDateStr(String date) {
        try {
          SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
          sdf.setLenient(false);
          sdf.parse(date);
        }
        catch (ParseException e) {
          return false;
        }
        catch (IllegalArgumentException e) {
          return false;
        }
        return true;
      }
    

提交回复
热议问题