Split the date and time

how to parse the date and time

Actual Value is : 15 Jun 2022 11:11:13
Expected Value is : 15 Jun 2022
How to split/Fetch/Parse the current date from Actual Value.

String fmt = “dd MMM YYYY HH:mm:ss”
DateFormat df = new SimpleDateFormat(fmt)
FInaldate dt = df.parse(date 1);
DateFormat dft = new SimpleDateFormat(“dd MMM YYYY”)

Below are a couple of ways to parse the “expected value” out of the “actual value”. It’s likely the Actual Value is already a String from a “getText()” or “getAttribute()”, so the bottom statement, with substring(), should suffice.

import java.text.SimpleDateFormat as SimpleDateFormat;


def date = new Date();

SimpleDateFormat wholeDate = new SimpleDateFormat("dd MMM YYYY HH:mm:ss")
String strDate = wholeDate.format(date);
SimpleDateFormat myDate = new SimpleDateFormat("dd MMM YYYY")

String finalDate = myDate.format(date);
	
println("we have ${finalDate}")
WebUI.comment("we have ${finalDate}")

"if we have the date as a String, then parse it out"
finalDate = strDate.substring(0,11)

println("we have ${finalDate}")
WebUI.comment("we have ${finalDate}")
1 Like

Or even:

import java.text.SimpleDateFormat as SimpleDateFormat;

def date = new Date();

SimpleDateFormat wholeDate = new SimpleDateFormat("dd MMM YYYY HH:mm:ss");
String strDate = wholeDate.format(date);

List<String> list = strDate.split(" ");
String finalDate = list[0] + " " + list[1] + " " + list[2];

println("we have ${finalDate}")
WebUI.comment("we have ${finalDate}")
1 Like