Home  >  Q&A  >  body text

java - SimpleDateformat 中可以使用正则表达式匹配吗?

PHP中文网PHP中文网2714 days ago587

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-04-17 15:48:02

    First, look at this document, look at this example, http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
    It shows that SimpleDateFormat can be combined by 'M', 'd', 'y' and other specified letters to match different time formats.

    I don’t know if the purpose of using regular expressions is to match times in different formats.
    If there are only one or several types, as long as you know the time format, it is nothing more than defining a few more SimpleDateFormat instances.

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

    If it is an uncertain time format, after using regular expressions, I feel that I don’t know how to parse the results.


    Update: November 30, 2015

    Able to parse regular expressions and convert regular expression metacharacters such as ?, ., $, ^There are certain rules for successful parsing, which is done by the regular expression engine. ?, ., $,^解析成功一定的规则,是正则表达式引擎完成的事情。

    这里的SimpleDateFormat似乎可以比方成正则表达式引擎,但是它的解析方式不一样,它不懂?,$,^,它也不会把?当做正则表达式的量词来处理,不会把$

    The SimpleDateFormat here seems to be likened to a regular expression engine, but its parsing method is different. It does not understand ?,$,< code>^, it will not treat ? as a quantifier of a regular expression, nor will it use $ to match the end of a line, etc. 🎜

    reply
    0
  • Cancelreply