Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape single quote in java's SimpleDateFormat

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,

like image 629
figaro Avatar asked Aug 13 '12 20:08

figaro


People also ask

How do you escape a single quote?

Single quotes need to be escaped by backslash in single-quoted strings, and double quotes in double-quoted strings.

How do you escape a single quote in Linux?

It's done by finishing already opened one ( ' ), placing escaped one ( \' ), then opening another one ( ' ). This syntax works for all commands.

How do you handle a single quote in a string?

Use escapeEcmaScript method from Apache Commons Lang package: Escapes any values it finds into their EcmaScript String form. Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.). So a tab becomes the characters '\\' and 't' .


2 Answers

This is what worked for me

SimpleDateFormat("dd MMM''yy", Locale.ENGLISH)

01 Jan'20
like image 134
Aalap Avatar answered Oct 06 '22 01:10

Aalap


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'}

like image 40
Jon Lin Avatar answered Oct 06 '22 01:10

Jon Lin