Forum Discussion

mpw83's avatar
mpw83
Contributor
6 years ago

How to add one month to a given date for Assertion

I want to assert a date from a response in JSON.  Expected Value is ApplicationDate + One Month Application date is like '2019-03-04'. So the Expected Date should be '2019-04-04'  I know there ...
  • Lucian's avatar
    6 years ago

    Hi again,

     

    Something like this works for me:

     

    import java.time.*
    import java.time.format.*
    
    def initialDateString = "2019-03-31" // this should be extracted dynamically but for the purpose of this example I guess it's ok
    
    // Create a LocalDate object
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
    LocalDate date = LocalDate.parse(initialDateString, formatter)
    
    date = date.plusMonths(1) // Update the date
    def modifiedDateString = date.format(formatter) // Create a String in case you need to process this date further

    Let me know if I can help you further!