Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails + How to add months to a Date

Tags:

grails

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.

like image 912
Ashish Joseph Avatar asked Jun 25 '13 06:06

Ashish Joseph


2 Answers

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.

like image 150
codelark Avatar answered Nov 05 '22 17:11

codelark


You can try :

d = new GregorianCalendar() 
d.setTime(new Date()) 
d.add(Calendar.MONTH,5) 
d.getTime() 
like image 5
Adrien.C Avatar answered Nov 05 '22 17:11

Adrien.C