Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add days to current date in groovy

Tags:

groovy

I am trying to get the current date and change the format and add 30 days to it, i tried the following in groovy :

def date = new Date().format("yyyy-MM-dd")
def laterdate = date + 30
log.info laterdate

I get the output as (formatting looks good)

Mon Jul 24 12:24:04 MST 2017:INFO:2017-07-2430

can someone please advise where i am doing wrong

like image 656
peter Avatar asked Jul 24 '17 19:07

peter


People also ask

How do I set the date in groovy?

Date Formatting In Groovy, you can use format function to format date object. You can see a quick example below. In this example, we simply parsed the date string into a date object on line 08 , and then we have formatted that date object by using format function on line 09 .


2 Answers

To add days:

Date date = new Date().plus(30)

To Substract days:

Date date = new Date().plus(-30)
like image 59
Gelberth Amarillo Rojas Avatar answered Sep 22 '22 02:09

Gelberth Amarillo Rojas


def today = new Date()
def yesterday = today + 30
log.info today.format("yyyy-MM-dd")
log.info yesterday.format("yyyy-MM-dd")
like image 31
peter Avatar answered Sep 21 '22 02:09

peter