0

Here is my code:

public VerifyTopicNotificationPage timeOfReceivedTopic()
{
    Date objDate = new Date(); // Current System Date and time is assigned to objDate
    SimpleDateFormat sdf;
    sdf = new SimpleDateFormat("E, MM dd, yyyy hh:mm");
    String dateString = sdf.format(objDate);
    System.out.println("Date in the format : "+dateString);
    Date dateObject = new Date();
    System.out.println("Syshour is : " + objDate.getHours() + "  " + "SysMin is : " + objDate.getMinutes());
    //try for notification
    int counter=0;
    Boolean flag=false;
    while((counter<10)&&(!flag)) {
        driver.navigate().refresh();
        seleniumUtil.explicitWait(2000);
        seleniumUtil.clickElement(clickOnBellIcon);
        seleniumUtil.explicitWait(4000);
        String s1="";
        String s2="";
        try {
           s1 = (driver.findElement(By.xpath("/html/body/div[3]/div/div/div/div[2]/div[2]/span")).getText())+(" ");
           s2 = s1.substring(0, 26).trim();
           System.out.println("Substring is " + s2);
           SimpleDateFormat format2 = new SimpleDateFormat("EEE, MMM dd, yyyy hh:mm a", Locale.US);
           Date date1 = null;
           try {
                date1 = format2.parse(s2);
                System.out.println("UI Date is : " + date1);
                System.out.println("UI hour is : " + date1.getHours());
                System.out.println("UI min is : " + date1.getMinutes());
           } catch (ParseException e) {
                e.printStackTrace();
            }
            if ((dateObject.getHours()==(date1.getHours())))
            {
                if (((dateObject.getMinutes() <= date1.getMinutes()) && (dateObject.getMinutes() <= dateObject.getMinutes()+10)) || ((dateObject.getMinutes() >= date1.getMinutes()) && (dateObject.getMinutes() >= dateObject.getMinutes()+10))) //3.55 -> 4.03 | 3.58
                {
                    System.out.println("Match found at " + s2);
                    flag = true;
                    break;
                }
            }
        }
        catch(Exception e)
        {
            System.out.println("encountered exception " + e);
        }
        counter++;
        seleniumUtil.explicitWait(60000);
    }
    Assert.assertTrue(flag);
    return PageFactory.initElements(driver, VerifyTopicNotificationPage.class);
}

Result of my code:

Date in the format : Tue, 02 02, 2021 09:45
Syshour is : 9  SysMin is : 45
encountered exception java.lang.StringIndexOutOfBoundsException: String index out of range: 26

I tried using trim() method but still it gives me the same exception.
How to resolve it?

1
  • Why on earth would you append a String and then substring it? What is this code supposed to do? s1.substring(0, 26).trim() Commented Feb 2, 2021 at 4:43

1 Answer 1

1
s2 = s1.substring(0, 26).trim();

This happens if the length of s1 is less than 26. First, you need to check the length of the s1 and then you can apply the substring() method based on the length of s1.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.