Scala/Lift 检查日期格式是否正确
我的电梯应用程序中有一个日期输入框,我想检查用户输入的日期格式是否正确:dd/mm/yyyy。
我怎样才能在scala中为此编写正则表达式检查?我看过模式匹配示例 - 但这似乎过于复杂。
PS:我不必使用正则表达式,欢迎使用任何其他替代品!
I have a date input box in my lift application, and I want to check that a user-entered date is in correct format: dd/mm/yyyy.
How can I write a regex check for this in scala? I've looked at pattern matching examples - but that seems over-complicated.
PS: I don't have to use regex, any other alternatives are welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
SimpleDateFormat
很丑陋,而且(更令人不安的是)非线程安全。如果您尝试在 2 个或更多线程中同时使用同一个实例,那么事情会以最令人不快的方式崩溃。JodaTime 要好得多:
如果它抛出
IllegalArgumentException
,则日期无效。我怀疑您会想知道实际日期(如果有效),因此您可能需要返回
Option[DateTime]
,如果无效则返回None
。或者,如果无法进行格式化,则可以使用
Either
捕获实际异常:UPDATE
要使用
Either
,您有两个主要策略:映射两侧之一:
折叠它:
当然,两者可以组合:
SimpleDateFormat
is ugly and (more disturbingly) non-thread-safe. If you try to simultaneously use the same instance in 2 or more threads then expect things to blow up in a most unpleasant fashion.JodaTime is far nicer:
If it throws an
IllegalArgumentException
, then the date wasn't valid.As I suspect you'll want to know the actual date if it was valid, you may want to return an
Option[DateTime]
, withNone
if it was invalid.Alternatively, use an
Either
to capture the actual exception if formatting wasn't possible:UPDATE
To then use the
Either
, you have two main tactics:map one of the two sides:
fold it:
Of course, the two can be composed:
正如用户未知所写,您应该使用一些知道如何正确处理日期的库,包括每个特定月份和闰年的天数。
SimpleDateFormat
对于字段的翻转来说不是很直观,并且最初仅通过翻转其他字段来接受错误的日期。为了防止它这样做,您必须对其调用setLenient(false)
。另请记住,SimpleDateFormat
不是线程安全的,因此您每次想要使用它时都需要创建一个新实例:或者您也可以使用 Joda Time 比 Java Date API 更直观,并提供线程安全的日期格式:
As user unknown has written you should use some library that knows how to handle dates correctly including days per specific month and leap years.
SimpleDateFormat
is not very intuitive regarding rollovers of fields and initially accepts wrong dates by just rolling over the other fields. To prevent it from doing so you have to invokesetLenient(false)
on it. Also keep in mind thatSimpleDateFormat
is not thread-safe so you need to create a new instance every time you want to use it:Alternatively you may also use Joda Time which is a bit more intuitive than the Java Date APIs and offers thread-safe date formats:
在对象中定义
DateTimeFormatter
实例是一个很好的做法,因为它是线程安全且不可变的。It's a good practice to define the
DateTimeFormatter
instance in an object since it's thread-safe and immutable.我不会使用正则表达式,而是使用 SimpleDateFormat (正如我们将看到的,这并不那么简单)。
处理允许 28 和 30 作为日期(但不允许 38)、不同的月份长度和闰年的正则表达式可能是一个有趣的挑战,但对于现实世界的代码而言并非如此。
(我假设 M 代表大月,而不是 m 代表小分钟)。
现在,让我们从一个错误开始:
hoppla - 它非常宽容,并且可以延续几个月到下一年。但我们可以通过第二个格式化过程来获取它:
我希望您不受正则表达式的束缚,并且可以使用它。
I wouldn't use a regex, but SimpleDateFormat (which isn't that simple, as we will see).
A regular expression which handles to allow 28 and 30 as day, but not 38, different month-lengths and leap years, might be an interesting challenge, but not for real world code.
(I assume M as in big Month, not m as in small minute).
Now, let's start with an error:
hoppla - it is very tolerant, and wraps months around to the next year. But we can get it with a second formatting process:
I hope you aren't bound to regex, and can use it.
由此我们可以验证字符串中的日期,并通过解析“dd/MM/yyyy”等格式来获取预期的日期响应。
from this we can validate date in string as well as we get the expected date response by parsing in the format like "dd/MM/yyyy".