[英]How to do date calculation in drools?
我刚刚流口水,并在drl文件中对日期比较感到困惑。 我有一个条件来比较两个日期类型的事实。 drl就像:
rule "TimeComparison"
when
$person:Person( date1 >= date2 )
then
//
end
在里面,date1是已知日期后的三天。 如何在drl规则文件中实现(特定日期之后的三天/几周/月)?
假设您的Date是java.util.Date,<运算符使用Date.before(date),>运算符使用Date.after(date)。
我建议使用Date.compareTo(),例如:
rule "TimeComparison"
when
$date
$person:Person( date1.compareTo(date2) >= 0)
then
//
end
如果date2不是你想要比较的东西,那么用内联替换所需的内容,例如“date1 is after now now”:
$person : Person(date1.compareTo(new Date()) >= 0)
使用java.time类更容易,例如使用LocalDate:
$person : Person(date1.plusDays(3).compareTo(date2))
如果比较日期是系统“已知”,则可能需要将计算的日期推断为新事实(这里我简单地构成了“PersonEffectiveDate”的概念)并在需要时使用它(更改方法/设计/根据您的情况需要的概念):
rule "Determine person effective date"
when
$person : Person($date1 : date1)
then
// do the desired calculation
Date $effectiveDate = $date1.plusDays(3);
PersonEffectiveDate ped = new PersonEffectiveDate($person, $effectiveDate);
insert(ped);
end
rule "TimeComparison"
when
PersonEffectiveDate($person : person, $effectiveDate : effectiveDate >= date2)
then
//
end
或者不同的东西,例如:
$person.date1.compareTo($effectiveDate) >= 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.