In my Grails project I have a date in the controller and I need to increment this date by one month, so I did as below:
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD HH:MM:ss");
def temp
use (TimeCategory)
{
temp=new Date()+30.days//current date 6-1-2016
}
println(sdf.format(temp))
this was the output:
2016-02-36
I tried plus(30)
also giving me the same result. Is there a way to do this increment correctly?
In a Java date format, D
stands for "Day in Year", hence 6+30 = 36
. You want to use d
for "Day in month".
You are also using Y
which is "Week year" instead of y
which is "year" and M
for minutes when you want m
.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")