mabuno
mabuno

Reputation: 13

How to add n minutes in groovy

I am working in SoapUI , which supports GroovyScript in TestCases.
In some TestCases i supposed to use a date of now + 15 minutes, 30, or 90 minutes.
If im using this script:

import java.util.Calendar;

def tdFormat = "yyyy-MM-dd HH:mm"
def today =  Calendar.getInstance()
def today15min = today.add(today.MINUTE,15)
def todayFormated = today15min.format(tdFormat)

gets NullPointerException: Cannot invoke method format() on null object error at line: 6.
How can i fix this?

Upvotes: 0

Views: 4376

Answers (2)

dmahapatro
dmahapatro

Reputation: 50245

Using TimeCategory.

use( groovy.time.TimeCategory ) {
    println 15.minutes.from.now.format( 'yyyy-MM-dd HH:mm' )
}

Upvotes: 5

doelleri
doelleri

Reputation: 19682

Calendar is a static class used to create Dates. Calendar.add() returns void, because it simply modifies the Calendar. You need to call getTime() to get a Date object which you can then format how you please.

Upvotes: 0

Related Questions