0

I wish to get 2018 replaced with 2014. Posting Year, which is currently having the 4/5/2018, must be changed to the purchase date of 4/5/2014 !! Please review the code:

String PostingPeriodIs=  Jan 2014
String PostingPeriod=  05/04/2018

int lnthPostingPeriodIs = PostingPeriodIs.length();
int lnthPostingPeriod = PostingPeriod.length();
String Year=null , YearIs=null;
try {
    Year= PostingPeriodIs.substring(lnthPostingPeriodIs-4);
    YearIs=PostingPeriod.substring(lnthPostingPeriod-4);
    System.out.println("Year is =========>"+Year);
    System.out.println("Expected year is ==>"+YearIs);
}
catch (Exception ex) {
    System.out.println("Exception in catchMethod");
    ex.printStackTrace();
}
String PostingYear= PostingPeriod.replace(Year, YearIs); 

uiDriver.webUIDriver.webDriver().findElement(By.xpath("//*[@id='trandate']")).sendKeys(PostingYear); 
Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
  • Maybe something like this: `String PostingPeriod = "05/04/2018".replace("2018", PostingPeriodIs.split("\\s+")[1]);`. – DevilsHnd - 退職した Apr 06 '18 at 10:41
  • Possible duplicate of [How to replace a substring of a string](https://stackoverflow.com/questions/16702357/how-to-replace-a-substring-of-a-string) – gzz Apr 06 '18 at 10:50

1 Answers1

0

Your mistake is here,

String PostingYear= PostingPeriod.replace(Year, YearIs);

.replace(toReplace,replacedWith)

Just interchange like below,

String PostingYear= PostingPeriod.replace(YearIs, Year);

Hope this helps

Code_Is_Law
  • 214
  • 2
  • 6