【发布时间】:2020-05-31 14:20:42
【问题描述】:
我有以下匿名块。我写了一个小的正则表达式来接受日期。它能够验证日、月和年。 现在我想考虑唯一的世纪年,正则表达式也应该允许 RR 格式的年(世纪年)。另外,我想接受前缀中没有零的一天;目前,它仅在当月接受这个。请看以下场景:
> 01/12/2154 (Do not accept, It's not a century year)
> 11/12/1996 (Do not accept, It's not a century year)
> 11/2/24 (Accept, and a plsql logic in IF condition could change it to 2024; Add 20 to it)
> 1/9/2020 (Accept)
> 01/02/2020 (Accept)
> 1/29/20 (Accept)
> 01/11/25 (Accept)
> 1/09/2021 (Accept)
**
declare
date_v varchar2(40):= '01/01/2025';
date_n date;
begin
if regexp_like (date_v, '([0-9]|1[0-2])/([0-2][0-9]|3[0-1])/[0-9]{4}') then
-- logic
date_n:= TO_DATE(date_v,'MM/DD/YYYY');
dbms_output.put_line(date_n);
end if;
end;
【问题讨论】:
-
一年内你不应该允许 两位 表示
YY,这就是引入 Y2K 错误 的原因。始终使用YYYY。坚持使用固定格式'YYYY-MM-DD'的 ANSI 日期文字 -
谢谢,拉利特。但是我怎样才能允许这种格式的日期 - 2020 年 1 月 1 日?你能看看我的正则表达式吗?