How to escape single quote in java's SimpleDateFormat

梦想与她 提交于 2019-12-12 11:15:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!