问题
I have a date input date like this: {ts '2012-08-13 02:30:01'}
I believe I can escape the invalid part with single quotes, but I have a single quote within the invalid part. How do I escape that? I tried a couple of patterns but it is not working.
Thanks,
回答1:
According to the javadocs for SimpleDateFormat
Text can be quoted using single quotes (') to avoid interpretation.
"''"
represents a single quote.
So a format string like:
"'{ts '''yyyy-MM-dd HH:mm:ss'''}'"
should match {ts '2012-08-13 02:30:01'}
回答2:
Have you tried escaping the single quote by another single quote? Otherwise the better way might be to exctract the relevant part (between the quotes) using a regular expression, and only then applying the SimpleDateFormat.
来源:https://stackoverflow.com/questions/11941807/how-to-escape-single-quote-in-javas-simpledateformat