Read date and add month

I reviewed the tutorials about adding time to dates, and most seem to be about getting the current time and then adding to that, whereas I need to add to a read date.

How do I get the date in this cell then add a month to it?

Example date = 10/4/2021 08:00
I need to add 1 month to this and then setText in the input field.

Hi @theo.retos, Try this: How to add days & years to current date using 'java.time.LocalDateTime'

The last bit you will need is to convert the “date” in your web page which will be a String into a Date object.

def myDate = WebUI.getAttribute(findTestObject('yourInput'), "value")

Now you have to convert the “myDate”, which will be a String, into a Date.

Converting a String to a Date in Groovy | Baeldung

So, convert the String into a Date then use the Date modifier to add your month, and then use the Formatter to convert back to a String for the setText function.

I don’t know what package I’m looking for or how to import it. https://www.baeldung.com/java-8-date-time-intro

Is this the right code right?

def currentDate = WebUI.getAttribute(findTestObject('Object Repository/Page_Work/Page_Work and Assignments/Page_Schedule Assignments/input_Schedule Start'), 'value')

println(currentDate)

def date = Date.parse('MM/d/yyy hh:mm', currentDate)

def newDate = date.add(Calendar.MONTH,1)

You may need the imports at the top of @Dave_Evers post to use the Date functions that he provides, but when I copied your code I get the “date” reference as a long, not a date.

Maybe like
def currentDate = WebUI.getAttribute(findTestObject('Object Repository/Page_Work/Page_Work and Assignments/Page_Schedule Assignments/input_Schedule Start'), 'value')

println(currentDate)  // this is a String

DateTimeFormatter pattern = DateTimeFormatter.ofPattern('MM/d/yyyy HH:mm');

def newdate = LocalDateTime.parse(currentDate, pattern)  // now we have a LocalDateTime

LocalDateTime futureDate = newdate.plusMonths(1);      // add a month

def nextMonth = futureDate.format(pattern)     // now we have a String again

println(nextMonth);   

@grylion54 – Edited the script to

//Get date from field, add one month, and setText
def currentDate = WebUI.getAttribute(findTestObject('Object Repository/Page_Work/Page_Work and Assignments/Page_Schedule Assignments/input_Schedule Start'), 'value')

println(currentDate)  // this is a String

DateTimeFormatter pattern = DateTimeFormatter.ofPattern('MM/d/yyyy HH:mm');

def newdate = LocalDateTime.parse(currentDate, pattern)  // now we have a LocalDateTime

LocalDateTime futureDate = newdate.plusMonths(1);      // add a month

String inputDate = futureDate.format(pattern)    // now we have a String again

WebUI.setText(findTestObject('Object Repository/Page_Work/Page_Work and Assignments/Page_Schedule Assignments/input_Schedule Start'), inputDate)

and that worked for 10/4/2021, however I get an error for 2/11/2022.

My dates are dynamic…is there a way for the program to recognize if the date are 1 or 2 digits in the pattern?

You have to change the pattern to only have a single month representation, then if there is one or two digits, this should work for both. Same for the hour part of the pattern, too.
DateTimeFormatter pattern = DateTimeFormatter.ofPattern('M/d/yyyy HH:mm');