简体   繁体   English

如何查找与当前日期比较的最新日期?

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

I created a list for dates in certain format. 我创建了某种格式的日期列表。 In that list I'm adding elements from some HTML code. 在该列表中,我要添加一些HTML代码中的元素。 Also I get curent time in format same as in list. 我也得到与清单相同的当前时间。

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());
    }

Now, how to get most recent passed date and compare with current date, because I must count how mach days have been passed? 现在,由于我必须计算已经过去了多少天,如何获取最近的通过日期并与当前日期进行比较?

You can do as follows : 您可以执行以下操作:

  • iterate over your elements 遍历您的元素
  • parse them to LocalDate (set the good pattern in the formatter ) parse它们parseLocalDate (在formatter设置良好的模式)
  • keep only the ones in the past 只保留过去的那些
  • get the max 获得max
  • print it 打印它

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);

Try using TreeSet.floor() / TreeSet.ceiling(). 尝试使用TreeSet.floor()/ TreeSet.ceiling()。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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