Ashish Joseph
Ashish Joseph

Reputation: 1153

Grails + How to add months to a Date

In Grails, is there a simple way to add/subtract months to/from a date?

We have methods like dateObj.add(10), which would add 10 days to dateObj. So my question is is there a way to add months.

Upvotes: 8

Views: 13865

Answers (2)

codelark
codelark

Reputation: 12334

The TimeCategory class provides a DSL for time manipulation:

import groovy.time.TimeCategory

use (TimeCategory) {
    twoMonthsFromNow = new Date() + 2.month
}

The documentation for TimeCategory can be found here.

Upvotes: 27

Adrien.C
Adrien.C

Reputation: 209

You can try :

d = new GregorianCalendar() 
d.setTime(new Date()) 
d.add(Calendar.MONTH,5) 
d.getTime() 

Upvotes: 5

Related Questions