简体   繁体   中英

Java code to compare date and time of an application with the system

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM