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

 

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the title of the post to something more descriptive? This will help people when searching for problems.
Watch a Video Interview
Top Kudoed Authors
Are You In?

Meet the Community Leaders!

SmartBear Connect is an excellent opportunity for training, in-person support, and networking.

GET YOUR TICKET WITH $100 OFF