如何在 p:column 中按日期类型进行 filterBy 操作。

huangapple go评论85阅读模式
英文:

How to filterBy on a Date type in a p:column

问题

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

请注意,我已经按照你的要求进行了翻译,只返回了翻译好的代码部分。如果你有任何其他问题或需要进一步的帮助,请随时提问。

英文:

i have a problem with filterBy when the data is a date but it works with other types of data,i used a filterFunction which i have already specified in my controller but it doesn't work nothing change when i filter. here is my code:

my .xhtml file:

&lt;p:column  filterBy=&quot;#{deploiement.date_deploiement_test}&quot; 
           sortBy=&quot;#{deploiement.date_deploiement_test}&quot;   headerText=&quot;date du d&#233;ploiement version_test&quot; filterFunction=&quot;#{listDeploiementController.filterByDate(value, filter, locale)}&quot;&gt;
                        &lt;h:outputText value=&quot;#{deploiement.date_deploiement_test}&quot;&gt; 
                                &lt;f:convertDateTime pattern=&quot;dd-MM-yyyy HH:mm&quot;  /&gt;
                                &lt;/h:outputText&gt;
                    &lt;/p:column&gt;

my controller:

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(&quot;dd-MM-yyyy HH:mm&quot;);
         

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

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

please if anyone know the solution please show me how i can fix it i really need it for my project.<br>

thank you.

答案1

得分: 1

你的代码存在两个问题:

  1. 错误地分割了 filterText
  2. 没有意识到一个 Date 对象可以在毫秒的差距下早于/晚于另一个,而你打算仅基于天、月、年、小时和分钟进行评估。

我建议你使用现代的 java.time 日期时间 API 和相应的格式化 API(包名 java.time.format),而不是过时且容易出错的 java.util 日期时间 API 和 SimpleDateFormat。从 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;
    }

    // 从 java.util.Date 获取 LocalDateTime
    Date filterDateValue = (Date) value;
    LocalDateTime filterDate = filterDateValue.toInstant().atOffset(ZoneOffset.UTC).toLocalDateTime();

    // 定义格式化器
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");

    // 在 ' - ' 上分割 filterText
    String[] parts = filterText.split("\\s-\\s");
    String fromPart = parts[0];
    String toPart = parts[1];

    // 将部分解析为 LocalDateTime 实例
    LocalDateTime dateFrom = null;
    LocalDateTime dateTo = null;
    try {
        dateFrom = LocalDateTime.parse(fromPart, formatter);
        dateTo = LocalDateTime.parse(toPart, formatter);
    } catch (DateTimeParseException e) {
        System.out.println("筛选条件中存在无效日期");
    }

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

There are two problems with your code:

  1. Splitting filterText incorrectly.
  2. Not realizing the one Date object can be before/after another one with a difference of even a millisecond whereas you intended to evaluate before/after only on the basis of day, month, year, hour and minute.

I recommend you do it using the modern java.time date-time API and the corresponding formatting API (package, java.time.format) instead of the outdated and error-prone java.util date-time API and SimpleDateFormat. Learn more about the modern date-time API from Trail: Date Time.

Assuming filterText is something like 10-12-2018 17:05 - 05-08-2020 11:25, I would do it as follows:

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(&quot;dd-MM-yyyy HH:mm&quot;);

	// Split filterText on &#39; - &#39;
	String[] parts = filterText.split(&quot;\\s-\\s&quot;);
	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(&quot;Invalid dates in the filter criteria&quot;);
	}

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

huangapple
  • 本文由 发表于 2020年8月31日 01:44:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63660344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定