【问题标题】:How to filterBy on a Date type in a p:column如何对 p:column 中的日期类型进行过滤
【发布时间】:2020-08-30 17:44:50
【问题描述】:

当数据是日期但它适用于其他类型的数据时,我遇到了 filterBy 的问题,我使用了我已经在我的控制器中指定的 filterFunction,但是当我过滤时它没有任何变化。这是我的代码:

我的 .xhtml 文件:

<p:column  filterBy="#{deploiement.date_deploiement_test}" 
           sortBy="#{deploiement.date_deploiement_test}"   headerText="date du déploiement version_test" filterFunction="#{listDeploiementController.filterByDate(value, filter, locale)}">
                        <h:outputText value="#{deploiement.date_deploiement_test}"> 
                                <f:convertDateTime pattern="dd-MM-yyyy HH:mm"  />
                                </h:outputText>
                    </p:column>

我的控制器:

private static final Logger LOG = Logger.getLogger(ListDeploiementController.class.getName());
      
       public boolean filterByDate(Object value, Object filter, Locale locale) {
           
          
           String filterText = (filter == null) ? null : filter.toString().trim();
           if (filterText == null || filterText.isEmpty()) {
               return true;
           }
           if (value == null) {
               return false;
           }
           DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm");
         

           Date filterDate = (Date) value;
          
           Date dateFrom;
           Date dateTo;
           try {
               String fromPart = filterText.substring(0, filterText.indexOf("-"));
               String toPart = filterText.substring(filterText.indexOf("-") + 1);
               dateFrom = fromPart.isEmpty() ? null : df.parse(fromPart);
               dateTo = toPart.isEmpty() ? null : df.parse(toPart);
           } catch (ParseException pe) {
               LOG.log(Level.SEVERE, "unable to parse date: " + filterText, pe);
               return false;
           }

           return (dateFrom == null || filterDate.after(dateFrom)) && (dateTo == null || filterDate.before(dateTo));
       }

如果有人知道解决方案,请告诉我如何修复它,我的项目确实需要它。

谢谢。

【问题讨论】:

  • 发布您的filterText 的示例。另外,您要在什么基础上决定之前/之后;是仅以日、月、年、小时和分钟为基础,还是以毫秒和纳秒为基础?

标签: java jsf primefaces primefaces-datatable


【解决方案1】:

你的代码有两个问题:

  1. 不正确地拆分filterText
  2. 没有意识到Date 对象可以在另一个对象之前/之后,甚至相差一毫秒,而您打算仅根据日、月、年、小时和分钟来评估之前/之后。李>

我建议您使用 modern java.time 日期时间 API 和相应的格式化 API(包,java.time.format)而不是过时且容易出错的 java.util 日期时间 API 和 @987654328 @。从 Trail: Date Time 了解有关现代日期时间 API 的更多信息。

假设filterText 类似于10-12-2018 17:05 - 05-08-2020 11:25,我会这样做:

public static boolean filterByDate(Object value, Object filter, Locale locale) {

    String filterText = (filter == null) ? null : filter.toString().trim();
    if (filterText == null || filterText.isEmpty()) {
        return true;
    }
    if (value == null) {
        return false;
    }

    // Get LocalDateTime from java.util.Date
    Date filterDateValue = (Date) value;
    LocalDateTime filterDate = filterDateValue.toInstant().atOffset(ZoneOffset.UTC).toLocalDateTime();

    // Define formatter
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");

    // Split filterText on ' - '
    String[] parts = filterText.split("\\s-\\s");
    String fromPart = parts[0];
    String toPart = parts[1];

    // Parse parts into LocalDateTime instances
    LocalDateTime dateFrom = null;
    LocalDateTime dateTo = null;
    try {
        dateFrom = LocalDateTime.parse(fromPart, formatter);
        dateTo = LocalDateTime.parse(toPart, formatter);
    } catch (DateTimeParseException e) {
        System.out.println("Invalid dates in the filter criteria");
    }

    return (dateFrom == null || filterDate.isAfter(dateFrom)) && (dateTo == null || filterDate.isBefore(dateFrom));
}

【讨论】:

  • 你的答案是个好主意,它只显示从最小到最大或相反的日期,但它不搜索指定的日期如果你有任何想法,我试图这样做请显示我。
  • @YACINEkaneiky - 我让你发布filterText 的示例,但你没有这样做,因此我认为它类似于10-12-2018 17:05 - 05-08-2020 11:25。这是filterText 的正确示例吗?
  • @YACINEkaneiky - 您的意思是该解决方案适用于某个范围,但您还需要一个特定日期的解决方案。是正确的理解吗?如果是,filterText 在特定日期包含什么内容?
  • 是的,我的 filterText 有点像 10-12-2018 17:05 ,是的,我需要一个特定日期的解决方案,比如 filterBy 对数字或字符串进行操作
  • filterText 将搜索示例,如果我们输入10-,我们将拥有包含数字10 的日期的所有日期。我希望它很清楚。
猜你喜欢
  • 1970-01-01
  • 2020-06-15
  • 1970-01-01
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
  • 2020-08-18
  • 2015-01-01
  • 2016-08-21
相关资源
最近更新 更多