Selenium:Java:Compare current time with a specific time

Hello There,
I need help in comparing two time(s) and validate a selenium test in Java
My requirement is - Validate if a counter is present on my application between 1:00am to 2:00pm
I am able to get the below
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(“HH:mm:ss”);
sdf.format(cal.getTime())
but not sure how to proceed with comparing “sdf.format(cal.getTime())” with > or < specific time
@kristof @mirza

4 Likes

Hi @muchapathi👋

I am not sure if I got you right. Is the question - how to compare time/date in Java or how to read the counter of your app?

I think the code snipet you provided just takes the system time and converts it to a given format.

1 Like

Hi @samuelm :wave:
To validate the presence of a counter(that has time) or in other words counter activated within an interval of time.
Something like below

1 Like

When comparing times (in other languages) I take the 2 datums, convert them to ints (Java has a huge 64bit int form by default here, so overflows are unlikely) and compare them, not sure java has a equivalent but I normally go (hour + minutes*60 + seconds*60*60) in a nice wrapper function on each and then compare.
Python has object.timestamp() which returns a variation of milliseconds - but you need to be sure to cater for timezone when doing so is my big warning on this one.

2 Likes

If you are checking times also be careful to fully consider the days around Daylight savings time. This can be problematic!

2 Likes

where can i discuss on this topic

2 Likes

This is how I have written the code:(BDD cucumber framework)

@Then(“user should see the Counter activated”)
public void userShouldSeeTheCounterActivated() {
String URL = SeleniumDriver.getDriver().getCurrentUrl();

    if (URL.equals(":https://www.ratioform.de"))  {
        String timeRange = "12:00-15:45";
        String[] timeR = timeRange.trim().split("-");
        LocalTime start = LocalTime.parse(timeR[0].trim());
        LocalTime end = LocalTime.parse(timeR[1].trim());
        LocalTime current = LocalTime.now();
        LocalTime currentHM = LocalTime.parse(current.getHour()+":"+current.getMinute());
        if(!currentHM.isBefore(start) && !currentHM.isAfter(end)) {
            Assert.assertTrue( rf_3317Actions.counterActivated(),"Counter is activated");
        }else {
            Assert.assertFalse(rf_3317Actions.counterActivated(),"Counter is not visible");
        }

    } else if (URL.equals(":https://www.ratioform.at")) {
        String timeRange = "12:00-14:00";
        String[] timeR = timeRange.trim().split("-");
        LocalTime start = LocalTime.parse(timeR[0].trim());
        LocalTime end = LocalTime.parse(timeR[1].trim());
        LocalTime current = LocalTime.now();
        LocalTime currentHM = LocalTime.parse(current.getHour()+":"+current.getMinute());
        if(!currentHM.isBefore(start) && !currentHM.isAfter(end)) {
            Assert.assertTrue( rf_3317Actions.counterActivated(),"Counter is activated");
        }else {
            Assert.assertFalse(rf_3317Actions.counterActivated(),"Counter is not visible");
        }

    } else if (URL.equals(":https://www.ratioform.ch")) {
        String timeRange = "12:00-11:30";
        String[] timeR = timeRange.trim().split("-");
        LocalTime start = LocalTime.parse(timeR[0].trim());
        LocalTime end = LocalTime.parse(timeR[1].trim());
        LocalTime current = LocalTime.now();
        LocalTime currentHM = LocalTime.parse(current.getHour()+":"+current.getMinute());
        if(!currentHM.isBefore(start) && !currentHM.isAfter(end)) {
            Assert.assertTrue( rf_3317Actions.counterActivated(),"Counter is activated");
        }else {
            Assert.assertFalse(rf_3317Actions.counterActivated(),"Counter is not visible");
        }

    }
}
1 Like

I’m not java expert, but this is refactorable, you have a block of code that takes 2 parameters

static void checkCounterStartEnd(LocalTime start, LocalTime end, LocalTime live) {
        LocalTime liveHM = LocalTime.parse(live.getHour()+":"+live.getMinute());
        if(!liveHM.isBefore(start) && !liveHM.isAfter(end)) {
            Assert.assertTrue( rf_3317Actions.counterActivated(),"Counter is activated");
        }else {
            Assert.assertFalse(rf_3317Actions.counterActivated(),"Counter is not visible");
        }  }

static void checkCounterRange(String range) {
String[] timeR = range.trim().split("-");
LocalTime start = LocalTime.parse(timeR[0].trim());
        LocalTime end = LocalTime.parse(timeR[1].trim());
        LocalTime current = LocalTime.now();
        checkCounterStartEnd(start,end,current);
}
if (url=) {// germany
        String timeRange = "12:00-15:45";
}
else { // italy
    String timeRange = "12:00-14:00";
}
checkCounterRange(timeRange);

My java is not accurate, but once you refactor it becomes easier to read/maintain this test code, and easier to change the comparison algorithm in just one place. As people have pointed out you want to always convert to local time and include the current DAY! If this test ran with an interval that crosses, BOOM. And that’s (midnight that-is) the most useful or only time to be running this kind of test, when it lies on a domain edge case.

1 Like

I am not a Java person but some info is in the link below, with examples.

2 Likes