Mallashetty Avinash
Mallashetty Avinash

Reputation: 21

how to increment date by 1. input is string type having date as a value

I have to develop a custom groovy code to take string as input, it would be a date. Increase the date by one, then return it. As the input is a string I would need help to handle this. thanks in advance for the help. for example. Input to the script - 05/12/2021 Output - 06/12/2021

Upvotes: 0

Views: 500

Answers (1)

ou_ryperd
ou_ryperd

Reputation: 2133

This is the Groovy way to do it:

def input = '05/12/2021'
def today = Date.parse('dd/MM/yyyy', input)
def tomorrow = today.plus(1)
assert tomorrow.format('dd/MM/yyyy') == '06/12/2021'

You can do all of that in one line too:

Date.parse('dd/MM/yyyy', '05/12/2021').plus(1).format('dd/MM/yyyy')

Upvotes: 1

Related Questions