I have a json string as below and I need to replace the datetime value with the current value . I have the datetime value from the string in one variable for e.g Date1 , and I have current date in another variable Date2 . I need java code to replace whole date , I know how to replace one word but here we have space in between the date and time so multiple words .
Date1 = Mon, 13 Jul 2020 14:08:30 GMT
Date2 = Wed, 15 Jul 2020 19:58:16 GMT
String json = {
"timestamp": [
{
"componentName": "docker-sam",
"datetime": "Mon, 13 Jul 2020 14:08:30 GMT"
}
]
"Id": "docker-sam",
"sourceId": " ",
}
I am doing :
vNewJson = json.replace(Date1,Date2);
But new Date2 is not getting replaced . Could anybody please suggest how to replace multiple words value in above case . Below is my code :
String[] Valuepair2 = vJson.split("datetime");
String vDate12 = Valuepair2[1].substring(2,Valuepair2[1].length());
String[] Valuepair3 = vDate12.split("GMT");
String vDate = Valuepair3[0] + " " + "GMT";
System.out.println(vDate);
String vDate1 = vDate.substring(2,vDate.length()); //remove curly
System.out.println("Value pair 0--->>" + vDate1);
StartEpochValue = vDate1;
DateTimeFormatter dtf = DateTimeFormatter.RFC_1123_DATE_TIME;
ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("GMT"));
String Date2 = (dtf.format(zdt)).toString();
System.out.println("Date 2 ==" + Date2);
System.out.println(vJsonfile1);
vPublishJsonfile1 = vJson.replace(vDate1,Date2);