I am into automation testing, selenium and java. I have been asked this question in an interview that suppose we have an application which displays date and time. How can we fetch that and compare with the system date and time to ensure that the application is showing the correct date and time.
Asked
Active
Viewed 196 times
2 Answers
0
First you have to get the date from webpage using getText() and then fetch the current date and time from the system using Date Class in java. Then compare both the strings.

NirmalKumar
- 24
- 4
-
1String comparison might make it hard to compare times which are only slightly different. Comparing the time as a `long` might be better for this. – Peter Lawrey Dec 26 '18 at 11:57
-
1The terrible `Date` class was supplanted years ago by the *java.time* classes. – Basil Bourque Dec 26 '18 at 16:01
0
Define a tolerance (say, a couple of seconds) and the expected time zone. Parse the date and time from the web app into an appropriate type from java.time, the modern Java date and time API using a DateTimeFormatter
. Use the now
method from the same class to get current time (for example, LocalDateTime.now(ZoneId.of("America/Sitka"))
). And and subtract your tolerance and use the isBefore
and/or isAfter
to determine whether the parsed date and time falls within the allowed.

Ole V.V.
- 81,772
- 15
- 137
- 161