Forum Discussion

SK07's avatar
SK07
Contributor
6 years ago

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

  • Lucian's avatar
    Lucian
    Community Hero

    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.

    • SK07's avatar
      SK07
      Contributor

      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.

      • Lucian's avatar
        Lucian
        Community Hero
        Ok, I will try to also help you with the Json extraction the next week. I am very busy though...