Date.format() no signature of method

@Keyword
public String generateEmail () {
	Date today = new Date();
	String todaysDate = today.format('YYYYMMdd');
	String nowTime = today.format('HHmmss');
	String email = ((('ua-' + todaysDate) + 'T') + nowTime) + '@mailinator.com';
	return email;
}

I use the above code in two different projects. In the first project, everything works as expected. In the second project(which was made more recently), Katalon does not recognize either .format() method, as shown by the runtime error. The entire class was copied from the working project. I also tried using the following code and it produced the same issue on the .format() methods, as suggested in Katalon Studio 7.9 rc - Bug in Date().format(<String>).

@Keyword
public String generateEmail () {
	Date today = new Date();
	SimpleDateFormat date = new SimpleDateFormat('YYYYMMdd');
	SimpleDateFormat time = new Date('HHmmss');
	String email = ((('ua-' + date.format(today)) + 'T') + time.format(today)) + '@mailinator.com';
	return email;
}

org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: com.MyKeywords.Date.format() is applicable for argument types: (java.lang.String) values: [YYYYMMdd]
Possible solutions: getAt(java.lang.String), print(java.io.PrintWriter), print(java.lang.Object)
at com.MyKeywords.Email.invokeMethod(Email.groovy)
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:50)
at Given_NoAccount_BookAPartyAsIntended.run(Given_NoAccount_BookAPartyAsIntended:98)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:369)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:360)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:339)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:331)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:248)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:133)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1626466343503.run(TempTestCase1626466343503.groovy:25)
Caused by: groovy.lang.MissingMethodException: No signature of method: com.MyKeywords.Date.format() is applicable for argument types: (java.lang.String) values: [YYYYMMdd]
Possible solutions: getAt(java.lang.String), print(java.io.PrintWriter), print(java.lang.Object)
at com.MyKeywords.Email.generateEmail(Email.groovy:10)
ā€¦ 14 more

You need to ensure you import the necessary resource file: How to add days & years to current date

You have probably noticed already that on the first line you correctly create a new SimpleDateFormat object, however, on the second line you arenā€™t. Fix that and then review @Dave_Evers link.

This message proves that you have a class com.MyKeywords.Date. This naming is horrible. You should never name your class as Date because it will confuse people.

When you write in a Test Case,

new Date('HHmmss')

You may expect to get an instance of com.MyKeyword.Date class, but others (@Dave_Evers, @grylion54, and I) would think you want to create an instance of java.util.Date. But the constructor signature Date(String s) was deprecated years ago, and would not work. So they would get confused.

If I were you, I would rename your class to be unique; for example, rename com.MyKeywords.Date to my.keywords.MyWonderfulDate.

I would advise you; you should no longer use java.util.Date and SimpleDateFormat class. These are decades old and poorly designed.

Java 8 introduced new (but already 7 years old) Date/Time API to replace the classic Date and Calendar API.

Java 8 Date/Time (java.time.LocalDateTime and java.time.format.DateTimeFormatter) is far better than the classic. No reason to continue using classics ( java.util.Date and java.text.SimpleDateFormat).

See the following article to learn the new API:

https://www.baeldung.com/java-8-date-time-intro

As @kazurayam reminded meā€¦
I should be using ā€˜java.time.LocalDateTimeā€™ and ā€˜java.time.format.DateTimeFormatterā€™
Going forward thatā€™s what I will be doing.

//Add these imports to the top of your test cases from script mode.
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

Here are some samples of what I will be using (they can be added to Keywords where needed).

//Get CurrentDateTime with different formatters
LocalDateTime cDateTime = LocalDateTime.now();
//DateTimeFormatter CurrentDate_formatter = DateTimeFormatter.ISO_DATE_TIME;
//DateTimeFormatter CurrentDate_formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
//DateTimeFormatter CurrentDate_formatter = DateTimeFormatter.ofPattern("MM-dd-yyyy-HH:mm:ss");
DateTimeFormatter CurrentDate_formatter = DateTimeFormatter.ofPattern("MM-dd-yyyy-hh:mm:ss");
def CurrentDateTime = cDateTime.format(CurrentDate_formatter);
println("CurrentDateTime: " + CurrentDateTime);
WebUI.comment("${CurrentDateTime}");
//Create FutureDateTime with different formatters
LocalDateTime fDateTime = localDateTime.plusYears(3);
//DateTimeFormatter FutureDate_formatter = DateTimeFormatter.ISO_DATE_TIME;
//DateTimeFormatter FutureDate_formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd:HH:mm:ss");
//DateTimeFormatter FutureDate_formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd:hh:mm:ss");
DateTimeFormatter FutureDate_formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
def FutureDateTime = fDateTime.format(FutureDate_formatter);
println("FutureDateTime: " + FutureDateTime);
WebUI.comment("${FutureDateTime}");
//Create 20 year-old-Birthdate
LocalDateTime bDateTime = localDateTime.minusYears(20)
//LocalDateTime bDateTime = localDateTime.minusYears(20).minusMonths(1).minusDays(1);
DateTimeFormatter BirthDate_formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
def BirthDateTime = bDateTime.format(BirthDate_formatter);
println("Birthdate: " + BirthDateTime);
WebUI.comment("${BirthDateTime}");

//Other Date Time Calculations methods:
plusYears()
plusMonths()
plusDays()
plusHours()
plusMinutes()
plusSeconds()
plusNanos()
minusYears()
minusMonths()
minusDays()
minusHours()
minusMinutes()
minusSeconds()
minusNanos()
1 Like