Need some help with date calculation

Query1:
Need to use a date 15 days ahead of current date in my current application.
Requirement is to get today’s date in mm/dd/yyyy format and then perform operation on it to get a formatted date=Current Date+15 days.
Since Date is dynamic, the field value should auto calculate and change.

Query2:
Need to use a unique name everytime.
An example: New Name= First Name+ Last Name+ Current DateTime-Stamp
Also, is there any way to assign/store the calculated value in a katalon variable.
Can someone please advise on these?

1 Like

Hi there,

Thank you very much for your topic. Please note that it may take a little while before a member of our community or from Katalon team responds to you.

Thanks!

You can use something like below:

import java.time.LocalDate
import java.time.format.DateTimeFormatter

DateTimeFormatter screenFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy")

LocalDate localDate = LocalDate.now()

gFutureDate = localDate.plusDays(15).format(screenFormat)

And you can “concatenate” Strings together, so your String could be:
newName = firstName + lastName + gFutureDate
or
newName = "${firstName}${lastName}${gFutureDate}"

Sample of other dates:
import java.time.LocalDate
import java.time.format.DateTimeFormatter

DateTimeFormatter screenFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy")

LocalDate localDate = LocalDate.now()

gScreenDate          = localDate.format(screenFormat)

gTomorrowDate        = localDate.plusDays(1).format(screenFormat)
gYesterdayDate       = localDate.minusDays(1).format(screenFormat)

gNextWeekDate        = localDate.plusDays(7).format(screenFormat)
gLastWeekDate        = localDate.minusDays(7).format(screenFormat)

gNextMonthDate       = localDate.plusMonths(1).format(screenFormat)
gLastMonthDate       = localDate.minusMonths(1).format(screenFormat)

gNextYearDate        = localDate.plusYears(1).format(screenFormat)
gLastYearDate        = localDate.minusYears(1).format(screenFormat)

Edit: I started thinking you may want this as a file name and I had another idea. The below allows you to make a “saved name” more than once a day, basically every time you run your test.

import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

DateTimeFormatter fileSaveFormat = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm");

LocalDateTime localDate = LocalDateTime.now()

gFutureDate = localDate.plusDays(15).format(fileSaveFormat).replace(':','').replace(' ','')

newFileName = firstName + lastName + gFutureDate
1 Like