本地日期时间未在程序中更新



我正在使用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()做。

如果你想在两个不同的时刻花时间,那么你真的应该使用 2 个变量(或常量(:

LocalDateTime now1 = LocalDateTime.now(); // First timestamp
// do some display of now1.format(dtf)
...
LocalDateTime now2 = LocalDateTime.now(); // Second timestamp
// do some display of now2.format(dtf)

最新更新