简体   繁体   English

Java正则表达式验证日期范围

[英]Java regex to validate range of dates

I having problems to generate a regex for a range of dates. 我在为某个日期范围生成正则表达式时遇到问题。 For example this range [2015-11-17, 2017-10-05] , How can I do? 例如这个范围[2015-11-17, 2017-10-05] ,我该怎么办? to validate if having a date belogns to that range using regex. 使用正则表达式验证是否有日期记录在该范围内。

And second question if is possible to have a generic regex which I can use for several range of date, only replacing few values in the regex with the new ranges I have, and the regex continues validating a range of dates , but with the new ranges. 第二个问题是,是否可以有一个通用正则表达式,我可以将其用于多个日期范围,仅用我拥有的新范围替换正则表达式中的几个值,并且该正则表达式会继续验证日期范围,但要使用新范围。 Thanks in advance for help =) 在此先感谢您的帮助=)

Do not use Regex 不要使用正则表达式

As the comments state, Regex is not appropriate for a range of dates, nor any span of time. 正如评论所指出的,正则表达式不适用于日期范围或任何时间跨度。 Regex is intended to be “dumb” in the sense of looking only at the syntax of the text not the semantics (the meaning). 正则表达式在仅查看文本语法而不是语义(含义)的意义上是“愚蠢的”。

java.time java.time

Use the java.time framework built into Java 8 and later. 使用Java 8和更高版本中内置的java.time框架。

Parse your strings into LocalDate objects. 将您的字符串解析为LocalDate对象。

LocalDate start = LocalDate.parse( "2015-11-17" );

Compare by calling the isEqual, isBefore, and isAfter methods. 通过调用isEqual,isBefore和isAfter方法进行比较。

Note that we commonly use the Half-Open approach in date-time work where the beginning is inclusive while the ending is exclusive. 请注意,我们通常在日期时间工作中使用“半开放式”方法,其中开始是包括在内的,而结尾是排除的。

These issues are covered already in many other Questions and Answers on Stack Overflow. 这些问题已在其他有关堆栈溢出的问题和解答中进行了介绍。 So I have abbreviated my discussion here. 因此,我在这里简化了我的讨论。

Just for completeness: You can actually use regular expressions to recognize any finite set of strings, such as a specific date range, however it would be more of an academic exercise than an actual recommended usage. 仅出于完整性考虑:实际上,您可以使用正则表达式来识别任何有限的字符串集,例如特定的日期范围,但是,与实际建议的用法相比,这更多的是学术练习。 However , if you happen to be programming some arcane hardware it could actually be necessary. 但是 ,如果您碰巧要对一些奥术硬件进行编程,则实际上是有必要的。

Assuming the input is always a valid date in the given format, the regex for your example could consist of: 假设输入始终是给定格式的有效日期,则示例的正则表达式可以包括:

2015-0[1-9].* - 2015 January to September 2015-0[1-9].* -2015年1月至9月

2015-10.* - 2015 October 2015-10.* -2015年10月

2015-11-0[1-9] - 2015 November 1 to 9 2015-11-0[1-9] -2015年11月1日至9日

2015-11-1[0-7] - 2015 November 10 to 17 2015-11-1[0-7] -2015年11月10日至17日

2016.* - all dates of 2016 2016.* - 2016.*年的所有日期

Add analogously for 2017, make a disjunction using | 类似地添加2017年,使用|进行分离。 (a|b|c|...), apply escaping of the regex implementation you use and then you have your date checker. (a | b | c | ...),对您使用的正则表达式实现进行转义,然后您便拥有了日期检查器。 If the input is not guaranteed to be a valid date it gets a bit more complicated but is still possible. 如果不能保证输入的日期是有效日期,则输入会变得有些复杂,但仍然可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM