0

I am working on Selenium Java, I need to get the following date format without the time, as a string in selenium java to validate whether it is up to date with the published date. I used getText() method from the website by splitting from the time and date. Is there any other best ways rather than this solution!

enter image description here

TinTin
  • 201
  • 1
  • 8
  • 19

2 Answers2

1

java.time

Edit: I have added more explanation and more code lines.

There’s a little challenge in the fact that the string on the website does not include year. One simple way to handle it is:

    ZoneId websiteTimeZone = ZoneId.of("America/Lower_Princes");
    DateTimeFormatter formatter
            = DateTimeFormatter.ofPattern("dd-MMM HH:mm", Locale.ENGLISH);

    String stringFromWebsite = "06-Feb 06:37";

    MonthDay today = MonthDay.now(websiteTimeZone);
    System.out.println("Today is             " + today);

    MonthDay date = MonthDay.parse(stringFromWebsite, formatter);
    System.out.println("Date from website is " + date);

    if (date.equals(today)) {
        System.out.println("It’s up to date");
    } else {
        System.out.println("It’s *NOT* up to date");
    }

When I ran today (March 12), the snippet printed:

Today is             --03-12
Date from website is --02-06
It’s *NOT* up to date

A MonthDay is a month and day of month without year. The advantage of using this class is we don’t need concern ourselves with year. A possible drawback is we can’t compare two such objects determine which one is before or after the other one. Such a comparison would require knowing the year of each one.

We need to know the time zone that the website uses since it is never the same date everywhere on Earth. Please insert the correct one where I put America/Lower_Princes.

I am parsing the string from the website into a MonthDay using a DateTimeFormatter with format pattern dd-MMM HH:mm since lower case d is for day of month, M is for month, H for hour of day and lower case m for minut of the hour. Since I am parsing into a MonthDay, the time is ignored (only its syntax still checked). In the print --03-12 means March 12 and --02-06 similarly February 6 (the date from the website). Since they are not the same, the code prints that the website is not up to date.

A more advanced solution might check if the date is a few days before or after today’s date and/or also look at the time.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Thanks for the answer. I ran the code and if I put other dates also, it is printing "It’s *NOT* up to date" Can u explain a bit how your code works? – TinTin Mar 12 '20 at 05:22
  • I have added more explanation and also a few more print statements in the code so you can better follow what happens (I hope) @Purushoth – Ole V.V. Mar 12 '20 at 19:02
0

You can use selenium's getText(), in order to acquire the value as a String. Afterwards you can use Java's DateTimeFormatter, to parse this date, and transform it to the format you want

gandalf_the_cool
  • 659
  • 2
  • 9
  • 23