我正在像这样在程序中使用LocalDateTimeDateTimeFormatter

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH-mm-ss");
LocalDateTime now = LocalDateTime.now();


我需要花一些时间来获取程序中的某些屏幕截图
但是当我使用它们时,我只有一个时间戳

这是我的其余代码

// SCREENSHOT
    JavascriptExecutor js = (JavascriptExecutor) driver;
    WebElement header = driver.findElement(By.xpath(
            "(.//*[normalize-space(text()) and normalize-space(.)='Button1'])[1]/following::nav[1]"));
    js.executeScript("arguments[0].setAttribute('style', 'position: static !important;')", header);
    Screenshot screenshot1 = new AShot()
            .shootingStrategy(ShootingStrategies.viewportPasting(ShootingStrategies.scaling(2f), 1000))
            .takeScreenshot(driver);
    Thread.sleep(5000);
    ImageIO.write(screenshot1.getImage(), "PNG", new File(Constants.ROUTE
            + "/asd/Asd/web/" + capName + now.format(dtf) + ".png"));
    Thread.sleep(1000);
    // // SCREENSHOT


这是我第一次拍摄。
我与now.format(dtf)连接
然后当我第二次拍摄

// SCREENSHOT
    JavascriptExecutor js1 = (JavascriptExecutor) driver;
    WebElement header1 = driver.findElement(By.xpath(
            "(.//*[normalize-space(text()) and normalize-space(.)='Button2'])[1]/following::nav[1]"));
    js.executeScript("arguments[0].setAttribute('style', 'position: static !important;')", header1);
    Screenshot screenshot2 = new AShot()
            .shootingStrategy(ShootingStrategies.viewportPasting(ShootingStrategies.scaling(2f), 1000))
            .takeScreenshot(driver);
    Thread.sleep(5000);
    ImageIO.write(screenshot2.getImage(), "PNG", new File(Constants.ROUTE
            + "/asd/Asd/web/" + capName + now.format(dtf) + ".png"));
    Thread.sleep(1000);
    // // SCREENSHOT


它打印出相同的时间戳,并且我的程序覆盖捕获

最佳答案

LocalDateTime是用于表示日期和时间(无时区)的类型。 LocalDateTime.now()返回表示当前日期和时间的LocalDateTime。您现在正在做的是调用LocalDateTime.now(),并期望您自己更新LocalDateTime,而您每次需要当前日期和时间时都应该执行LocalDateTime.now()

关于java - LocalDateTime不在程序内更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57825403/

10-12 22:33