简体   繁体   中英

Extract date from a string in java

I have a string as following :

String str = "skip=anthing&id='234234432234' and (timestamp le 2017-01-17T05:05:55:358Z and timestamp ge 2017-01-17T08:05:55:358Z)&format=json";

I want to extract the date part from the string put it inside single quotes and replace it back to original string.

The resultant string should be

String str = "skip=anything&id='234234432234' and (timestamp le '2017-01-17T05:05:55:358Z' and timestamp ge '2017-01-17T08:05:55:358Z')&format=json"

You might use the following regex

String input = "skip=anthing&id='234234432234' and (timestamp le 2017-01-17T05:05:55:358Z and timestamp ge 2017-01-17T08:05:55:358Z)&format=json";
String output = input.replaceAll("(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}:\\d{3}Z)", "'$1'");
System.out.println(output); // skip=anthing&id='234234432234' and (timestamp le '2017-01-17T05:05:55:358Z' and timestamp ge '2017-01-17T08:05:55:358Z')&format=json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM