Alex
Alex

Reputation: 3

Date selector based on months in Java

I have code in Java for Selenium Webdriver TestNG. Is for comparing if the date on the website is same as today date. Problem is that date on webpage dateOnWebpage for 11th April 2018 is in format

Today 4/11/2018

So I made selecter to compare date formats if months < 10 if(javaDateSelector < 10) than date to compare is in format M/dd/yyyy else is in format MM/dd/yyyy.

Is there better way to code it than I made it? Because I needed to parse date to string and than to int to compare it and code is quite long.

@Test(priority=3)
 public void test3DateCheck() throws Exception
 {

  String dateOnWebpage = driver.findElement(By.xpath("//div[@id='homeCalendarSection']/div/div[2]/table/tbody/tr/td/div/ul/li")).getText();

  System.out.println("Today Date on webpage is : " + dateOnWebpage);

  //DateFormat dateFormat1 = new SimpleDateFormat("M/dd/yyyy");
  DateFormat dateFormat1 = new SimpleDateFormat("MM");
  Date date = new Date();
  String javaDate1 = dateFormat1.format(date);
  int javaDateSelector = Integer.parseInt(javaDate1);

  if(javaDateSelector < 10)
  { 
      DateFormat dateFormat2 = new SimpleDateFormat("M/dd/yyyy");  
      String javaDate2 = dateFormat2.format(date);
      System.out.println("Today Date from Java is : " + javaDate2);
      Assert.assertEquals(dateOnWebpage, "Today " + javaDate2);
  }
  else
  {
      DateFormat dateFormat3 = new SimpleDateFormat("MM/dd/yyyy");  
      String javaDate3 = dateFormat3.format(date);
      System.out.println("Today Date from Java is : " + javaDate3);
      Assert.assertEquals(dateOnWebpage, "Today " + javaDate3);
  }

}

Upvotes: 0

Views: 57

Answers (1)

Anonymous
Anonymous

Reputation: 86203

If you don’t care whether the date on the web page is written with leading zero for month and day of month or not and just want to test whether the date is correct:

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("'Today' M/d/uuuu");
    LocalDate today = LocalDate.now(ZoneId.of("Pacific/Norfolk"));
    System.out.println("Today is " + today);
    LocalDate observedDate = LocalDate.parse(dateOnWebpage, dateFormatter);
    Assert.assertEquals(today, observedDate);

Rather than testing the string I am parsing the date and testing it. Even though the pattern has one M and one d in it, parsing two-digit months and two-digits day of month poses no problem.

If on the other hand you also want to test that the date on the web page is written without any leading zeroes, it’s best to test the string, like you already did:

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("M/d/uuuu");
    LocalDate today = LocalDate.now(ZoneId.of("Pacific/Norfolk"));
    String todayString = today.format(dateFormatter);
    System.out.println("Today is " + todayString);
    Assert.assertEquals("Today " + todayString, dateOnWebpage);

Again, even though the pattern has one M and one d in it, two digits will be printed if the month or the day of month is greater than 9. What else could the format method do? If you require two-digit day of month always, put dd in the format pattern string.

In both snippets please fill in your desired time zone where I put Pacific/Norfolk since it is never the same date everywhere on the globe.

I am using and recommending java.time, the modern Java date and time API. DateFormat and SimpleDateFormat are not only long outdated, they are also notoriously troublesome. Date is just as outdated. I would avoid those classes completely. The modern API is generally so much nicer to work with.

Link: Oracle tutorial: Date Time explaining how to use java.time.

Upvotes: 1

Related Questions