正则表达式,用于SimpleDateFormat字符串

编程入门 行业动态 更新时间:2024-10-25 12:25:28
本文介绍了正则表达式,用于SimpleDateFormat字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我主要以 yyyy-MM-dd 和 yyyy-MM-dd hh:mm:ss 的形式约会.我想模式匹配 yyyy-MM-dd hh:mm:ss .

I have dates mainly in the forms of yyyy-MM-dd and yyyy-MM-dd hh:mm:ss. I want to pattern match yyyy-MM-dd hh:mm:ss.

为此目的编写正则表达式很简单吗?我是regex的新手,所以不胜感激任何资源达人都可以让我快速入门吗?

Is it trivial to write an regex for this purpose? I am new to regex so would appreciate any resource tat could get me up to speed?

推荐答案

如果要在字符串中查找这些日期符号,则正则表达式确实是一个不错的选择.您可以使用此正则表达式:

If you are trying to find these date notations in a String, then regexes is indeed a good choice. You could use this regex:

\b\d{4}-\d{2}-\d{2} \d{1,2}:\d{2}:\d{2}\b

在此处查看操作: rubular/r/qZOTsUikbo .注意:这与"日期"匹配,例如以及 9999-99-99 99:99:99 .如果这对您来说是个问题,则可以在找到潜在的字符串后对其进行验证.

See it in action here: rubular/r/qZOTsUikbo. Note: this matches "dates" like 9999-99-99 99:99:99 as well. If that is a problem for you, you could verify them after you found the potential Strings.

如果要解析它们,请使用SimpleDateFormat.

If you want to parse them, then use a SimpleDateFormat.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = sdf.parse("2013-12-24 18:31:20");

更多推荐

正则表达式,用于SimpleDateFormat字符串

本文发布于:2023-11-03 12:57:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555191.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   正则表达式   SimpleDateFormat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!