Search code examples
dategroovyiteratorsoapui

groovy next() date issue


I am trying to add a groovy script in SoapUI to find tomorrow's date using next() in current date. I am getting the date as expected for all other dates except if the date is 19.

def TodaysDate = new java.util.Date().format("yyyy-MM-dd")
log.info ">>>>>>>>>> TodaysDate="+TodaysDate
log.info TodaysDate.next()

Output:

Wed Jul 19 14:34:29 EDT 2017:INFO:>>>>>>>>>> TodaysDate=2017-07-19
Wed Jul 19 14:34:29 EDT 2017:INFO:2017-07-1:

I tried this also.

def Today = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date())
log.info Today
NextDay = Today.next()
log.info NextDay 

Output:

Wed Jul 19 14:43:38 EDT 2017:INFO:2017-07-19
Wed Jul 19 14:43:38 EDT 2017:INFO:2017-07-1:

This next() iterator works fine for other dates. Can you help me understand what I am doing incorrect here?


Solution

  • The format() method returns a String. And when you call next() on a String, it increments the last character. So, character 9 is incremented to the next unicode value, becoming :.

    If you want your dates in a specific format, first you call next() in a Date object, then you format it:

    def TodaysDate = new java.util.Date()
    log.info ">>>>>>>>>> TodaysDate="+TodaysDate.format("yyyy-MM-dd")
    log.info TodaysDate.next().format("yyyy-MM-dd")
    

    The will print TodaysDate=2017-07-19 and the next date as 2017-07-20.