This is the JSON array. I want to get the last object and then compare the date updatedAt
, which we get new data from the user and compare if 5 minutes are up then not allowed to update. How can I achieve this in java?
[{"content":" heyyyyy how are you !!! ......well or not ???? ","updatedAt":1632313553895},{"content":" heyyyyy how are you !!! ......well or not ???? ","updatedAt":1632313843305}]
CodePudding user response:
You can do it as below. As 5min is 30000ms final comparison is uses. While other steps are clear with their variable names.
String jsonArrayString = " [{\"content\":\" heyyyyy how are you !!! ......well or not ???? \",\"updatedAt\":1632313553895},{\"content\":\" heyyyyy how are you !!! ......well or not ???? \",\"updatedAt\":1632313843305}]";
JSONArray jsonArray = new JSONArray(jsonArrayString);
JSONObject lastJsonObject = (JSONObject) jsonArray.get(jsonArray.length() -1);
String updatedAt = lastJsonObject.getString("updatedAt");
boolean isDifferenceFiveMinute = ((new Date()).getTime() - Long.valueOf(updatedAt)) > 300000; // 300000ms = 5 min