Correct Date is not getting added when adding a date to a field

Hi, I am using the below lines of code to add a date to a field but it seems the date is getting concatenated with last two digits if Year.
String format = “DD/MM/YYYY”

SimpleDateFormat sdf = new SimpleDateFormat(format)

String date = sdf.format(new Date()+30)

My expected result will be date should added as current date + 30 days but when running the script , it is adding the date as 165/06/2020.
Can you please advise!

hi,
try with Calendar class

import java.text.DateFormat
import java.text.SimpleDateFormat

DateFormat dateFormat = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”);
Date currentDate = new Date();
// convert date to calendar
Calendar c = Calendar.getInstance();
c.setTime(currentDate);
// manipulate date
c.add(Calendar.YEAR, 1);
c.add(Calendar.MONTH, 1);
c.add(Calendar.DATE, 1); //same with c.add(Calendar.DAY_OF_MONTH, 1);
c.add(Calendar.HOUR, 1);
c.add(Calendar.MINUTE, 1);
c.add(Calendar.SECOND, 1);

// convert calendar to date
Date currentDatePlusOne = c.getTime();

System.out.println(dateFormat.format(currentDatePlusOne));

2021/06/15 23:18:27

Here are some date formats that I use:

Date todaysDate = new Date();

GlobalVariable.gScreenFormattedDate = todaysDate.format(“MM/dd/yyyy”);
GlobalVariable.gFormattedYesterdayDate = todaysDate.plus(-1).format(“MM/dd/yyyy”);

You would want:
nextMonth = todaysDate.plus(30).format(“MM/dd/yyyy”);

I made a demo

  1. The script needs the datetime value of TODAY. I will use java.time.LocalDate class to get it.
  2. The LocalDate class provides plusDays(int n) and other convenient methods. We can calculate the date of Tomorrow ( today.plusDays(1) ) or Yesterday ( today.minusDays(1) ) just easily.
  3. Datetime value displayed in a web page is formatted in various ways. Often date display is tailored according to Locale. E.g, in Japan, I may see 令和2年5月16日. I need to format the today’s date to cope exactly with the format applied in the web page. I will use java.time.format.DateTimeFormatter to format the instance of java.time.LocalDate into a String.

Still i am gertting the below error.No idea why date is displaying as 3 digits .
image

Three people have given some information on various methods of getting Date future and past. What format of Date did you go with?

Or did you stay with:
String date = sdf.format(new Date()+30)
Because this will not work!

I tried the code suggested by you but it did not work , error message displaying for .
gScreenFormattedDate

And also i tried imo_Kuisma1’'s code( tried with current date) but wrong date( same as displayed in the screenshot) added to the date fields .

In both cases it is not working.

Okay, I would be interested to know what the error message was–if it was to do with the date’s format or not having a declared variable. I am going to assume it was the date’s format that was incorrect. So, you can start by something like (note the swap in position of day and month):

Date todaysDate = new Date();

def formattedDate = todaysDate.format(“dd/MM/yyyy”);

As I have above, for a month ahead, you could use:

def nextMonth = todaysDate.plus(30).format(“dd/MM/yyyy”);

Both “formattedDate” and “nextMonth” are Strings that you can put into the field, like:

WebUI.setText(findTestObject(‘my page/an element’), formattedDate)

WebUI.setText(findTestObject(‘my page/another element’), nextMonth)

And then to check the date is put into the field:

WebUI.verifyMatch(WebUI.getAttribute(findTestObject(‘my page/another element’’), ‘value’), nextMonth, false))

Hope that helps.

I can tell you why. You have this line:

String format = “DD/MM/YYYY”

DD means day in year. Today is 24th of May, 2020. Today can also be expressed as 145th day of 2020. So DD gives you 145.

See the javadoc of SimpleDateFormat.

You do not want 145th of the year 2020. You rather want 24th of May 2020. Then you should write dd (day in month):

String format = "dd/MM/yyyy"

Copy and paste the following code as a Test Case in KS and run it. You can see how SimpleDateFormat works.

import java.text.SimpleDateFormat

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

def today = new Date()
def other = today + 30

def sdf1 = new SimpleDateFormat("DD/MM/YYYY") // DD means day in year
WebUI.comment("today=${sdf1.format(today)}")  // today=145/05/2020 
WebUI.comment("other=${sdf1.format(other)}")  // other=175/06/2020


def sdf2 = new SimpleDateFormat("dd/MM/yyyy") // dd means day in month
WebUI.comment("today=${sdf2.format(today)}")  // today=24/05/2020
WebUI.comment("other=${sdf2.format(other)}")  // other=23/06/2020


// See Javadoc of java.text.SimpleDateFormat
// https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html