簡體   English   中英

如何查找與當前日期比較的最新日期?

[英]How to find most recent date compare to current date?

我創建了某種格式的日期列表。 在該列表中,我要添加一些HTML代碼中的元素。 我也得到與清單相同的當前時間。

List<WebElement> times = driver.findElements(By.tagName("time"));
String timeStamp = new SimpleDateFormat("MMM d, yyyy", Locale.ENGLISH).format(Calendar.getInstance().getTime());

System.out.println(timeStamp);
for (WebElement element: times) {
        System.out.println(element.getText());
    }

現在,由於我必須計算已經過去了多少天,如何獲取最近的通過日期並與當前日期進行比較?

您可以執行以下操作:

  • 遍歷您的元素
  • parse它們parseLocalDate (在formatter設置良好的模式)
  • 只保留過去的那些
  • 獲得max
  • 打印它

List<WebElement> times = driver.findElements(By.tagName("time"));

LocalDate now = LocalDate.now();

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH);

LocalDate mostRecent = times.stream()
            .map(webE -> LocalDate.parse(webE.getText(), formatter))
            .filter(date -> date.isBefore(now))
            .max(Comparator.comparingLong(LocalDate::toEpochDay))
            .orElseThrow(IllegalAccessException::new);

System.out.println(mostRecent);

LocalDate dateBefore;
LocalDate dateAfter;
long daysBetween = DAYS.between(dateBefore, dateAfter);

嘗試使用TreeSet.floor()/ TreeSet.ceiling()。

https://www.tutorialspoint.com/java/util/treeset_floor.htm

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM