cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy: compare the date format

Contributor

Groovy: compare the date format

I want to compare the date from json 1 & json 2 and display if its match or not in this array.

Json1 : {"prods": [{"Item":"11","date": "2018-08-08"}
Json2: {"prods": [{"Item":"12","date": 1533700800000}

4 REPLIES
Community Leader

Re: Groovy: compare the date format

This is the code that allows you to compare the dates:

 

import java.text.SimpleDateFormat

// Get timestamp
def timestamp = 1533700800000
// Set a date using the timestamp
def date = new Date( timestamp )
// Create a date format as desired
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" )

// Get the other date
secondDate = "2018-08-08"

assert dateFormat.format( date ).equals(secondDate) 

Let me know if you also need help with the JSON extraction. I am a little busy now so I can't promise anything though.



If my answer helped please click on the 'Accept as Solution' button.
Highlighted
Contributor

Re: Groovy: compare the date format

@Lucian, I wanted to read the date(milliseconds) from each array of Json1(convert this date to yyyy-mm-dd)

and compare it with Json2 date (yyyy-mm-dd) of each array.

Community Leader

Re: Groovy: compare the date format

Ok, I will try to also help you with the Json extraction the next week. I am very busy though...


If my answer helped please click on the 'Accept as Solution' button.
Community Leader

Re: Groovy: compare the date format

Hey @SK07

 

 

I typed out a huge response and then I realised my response wouldn't achieve what you wanted - I've got a script courtesy of @msiadak that compares 2 attribute values from 2 different responses and works really quite nicely - unfortunately I didn't read your post properly - the script won't interpret datetimestamp and date values - so sorry - I've wasted your time on this.

Only reason Ive even replaced my message with this is cos I don't know how to delete a whole message!

 

cheers,

 

rich

 

API Summer News

Congratulate last week's winners here:
1. @sanj - $50
2. @Sehuang and @aaronpliu - $35
3. @jhanzeb1 - $15

To do this week:
- Ask product-related questions
- Reply to any questions

Win $50, $35 or $15 gift card!
Winners will be decided next Monday (Aug 27)

Top Kudoed Authors
Are You In?