0

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);    
oguz ismail
  • 1
  • 16
  • 47
  • 69
Ronak
  • 3
  • 2
  • i dont unterstand your question... please try to explain better – verity Jul 15 '20 at 20:30
  • 1
    Are Date1 and Date2 Strings? – verity Jul 15 '20 at 20:34
  • 1
    This isn't a [mcve]. –  Jul 15 '20 at 20:35
  • Do you _have_ the original value `date1`, or you need to _find_ it first and then replace with current date? – Nowhere Man Jul 15 '20 at 20:45
  • I get my Date1 as string , Date2 is fetched as below : DateTimeFormatter dtf = DateTimeFormatter.RFC_1123_DATE_TIME; ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("GMT")); System.out.println(dtf.format(zdt)); .......... Then I do json.replace(Date1," "+ dtf.format(zdt)); ...............This is not getting replaced . – Ronak Jul 15 '20 at 21:11
  • Apparently a follow-up question to [How can I get the current date and time in below format , date = “Mon, 13 Jul 2020 14:08:30 GMT”](https://stackoverflow.com/questions/62921667/how-can-i-get-the-current-date-and-time-in-below-format-date-mon-13-jul-20) – Ole V.V. Jul 16 '20 at 03:04

2 Answers2

4

Well, this should be working. I tried it here, on a sample application, and got the desired result. There must be something missing in your code. Are you escaping the double quotes?

This is the code i ran:

public class Application {
    public static void main(String[] args) {
        String date1 = "Mon, 13 Jul 2020 14:08:30 GMT";
        String 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\": \"5765\"";
        System.out.println(json.replace(date1, date2));
    }
}

---- [Edit] ----

Ok, now i think i got it. There's two spaces between the hour and the GMT word. The problem is here:

String[] valuepair3 = vDate12.split("GMT"); 
String vDate = valuepair3[0] + " " + "GMT";

You're puting an extra space on the second line.

  • 2
    Another thing: when dealing with json objects, i strongly recommend you to use a proper object from a library like org.json for example. You can see more about it here: https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – Sarah Rodrigues Jul 15 '20 at 20:45
  • 1
    Or [Jackson](https://github.com/FasterXML/jackson), or [Gson](https://github.com/google/gson). – dnault Jul 15 '20 at 21:04
  • Hi Sarah , yes I am escaping double quotes . My values are Mon, 13 Jul 2020 14:08:30 GMT and Wed, 15 Jul 2020 21:17:21 GMT . – Ronak Jul 15 '20 at 21:18
  • 1
    Can you post your code on the question? So we can see if there's anything wrong. – Sarah Rodrigues Jul 15 '20 at 21:23
  • copied , vDate1 is the date extracted from json file , Date2 is the current date taken from system . – Ronak Jul 15 '20 at 21:28
  • 1
    The problem is here: String vDate = valuepair3[0] + " " + "GMT". – Sarah Rodrigues Jul 15 '20 at 21:50
0

First, as Sarah Rodrigues said in a comment, you should use a JSON library for manipulating your JSON. You should not do string manipulation.

If you don’t want the library, I’d use a regular expression (borrowing the partial JSON string from Sarah Rodrigues’ answer for demonstration):

    String json = "{\"timestamp\": [ { \"componentName\": \"docker-sam\", \"datetime\": \"Mon, 13 Jul 2020 14:08:30 GMT\" }  ] " +
            "\"Id\":\"docker-sam\",  \"sourceId\": \"5765\"";

    String currentDateTime = ZonedDateTime.now(ZoneOffset.UTC)
            .format(DateTimeFormatter.RFC_1123_DATE_TIME);
    String newJson = json.replaceAll("(\"datetime\": \")[A-Za-z0-9, :]+(\")",
                                     "$1" + currentDateTime + "$2");

    System.out.println(newJson);

Output when running just now:

{"timestamp": [ { "componentName": "docker-sam", "datetime": "Thu, 16 Jul 2020 03:11:00 GMT" } ] "Id":"docker-sam", "sourceId": "5765"

In the regular expression round brackets enclose groups that I can resue in the replacement string. In the replacement string I refer to them as $1 and $2. [A-Za-z0-9, :]+ matches a non-empty run of the characters mentioned inside the square brackets. I put the characters that your date string can consist of for it to match the content of that string.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161