How do you format or verify date that follows with th, st, nd, rd, etc?

Tuesday, Nov 21*st* 2023' - i’ve tried everything to format or at least verify that string with date. Can’t seem to do it? Losing my mind here. is there anything i can add to the ‘dd’?

SimpleDateFormat simpleDate = new SimpleDateFormat("EEEE, MM *dd* yyyy");

2 Likes

Maybe you can use something from the below page:

2 Likes

Yes, I saw that before posting it here. Those methods just feels like overkill just to verify the last 2 char of a date. There’s gotta’ be something else that someone else knows/does.
I wonder if I can even take that string and convert ToDate or something in a format stated above?

1 Like

Here is another that is not a single “format” but maybe create a Keyword for it so that in your code it becomes one. You will have to play with getting the “pattern” format as you want it to display.

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

LocalDate localDate = LocalDate.now()
def dayPart = localDate.getDayOfMonth()

DateTimeFormatter royalFormat = DateTimeFormatter.ofPattern("MMM d'th' yyyy")

switch (dayPart) {
	 case 1:  case 21:  case 31:
		royalFormat = DateTimeFormatter.ofPattern("MMM d'st' yyyy")
		  break;
	  case 2: case 22:
		royalFormat = DateTimeFormatter.ofPattern("MMM d'nd' yyyy")
		  break;
	  case 3: case 23:
		royalFormat = DateTimeFormatter.ofPattern("MMM d'rd' yyyy")
		  break;
}

gRoyalDate = localDate.format(royalFormat)

println gRoyalDate
WebUI.comment(gRoyalDate)
1 Like

maybe we can even add the default so remaining cases are returned as “th”?

oh n/m. i missed it was covered in your codeblock

switch (dayPart) {
		 case 1:  case 21:  case 31:
			royalFormat = DateTimeFormatter.ofPattern("MMM d'st' yyyy")
			  break;
		  case 2: case 22:....
			
		  default:
		    royalFormat = DateTimeFormatter.ofPattern("MMM d'th' yyyy")			
			  break;
	}

This seems to be supported in Java 8 with the followng formatter from what I found on the internet. i could be wrong.

MM Q, yyyy"

where Q returns the date with the 2 added char after date. Isn’t katalon v9 running on Java 17? :face_with_raised_eyebrow:

@ashraf.madina

You should read the javadoc of java.time.format.DateTimeFormater, which explains the Date/Time formatting feature built-in the JDK8 and higher.

https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

Q/q     quarter-of-year             number/text       3; 03; Q3; 3rd quarter

The Q does not give you st, nd, rd, th for dates.

As the javadoc tells, it does not support the formatting/parsing the st, nd, rd, th for dates. So you need to invent a method for yourself, though it may look overkilling for you. How to? … @grylion54 and others already showed you.

yea, someone posted that Q as the answer but didn’t really look right, i already did the switch-case one… which works fine, but was just fishing for something with less lines of code (if there was any).
doesn’t seem like it. :smiley:

1 Like