英文:
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:
<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>
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("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));
}
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
你的代码存在两个问题:
- 错误地分割了
filterText
。 - 没有意识到一个
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:
- Splitting
filterText
incorrectly. - 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("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));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论