用 Camel 的表达语言获取和格式化昨天的日期 [英] Get and format yesterday's date in Camel's expression language
问题描述
我在 Camel 的路径中使用日期:
I am using the date in a path in Camel:
fileName=${date:now:dd-MM-yyyy}
但我现在需要的是 - 1 天.这可能吗?
but what I need is now - 1 day. Is that possible?
推荐答案
好吧,不直接.简单语言中的 date: 对象只能获取当前时间(或您放置在标头中的某个时间值 - 您可以在 java 或类似语言中执行此操作.
Well, not directly. The date: object in the simple language can only grab the current time (or some time value you have placed inside a header - which you could do in java or similar.
但你也可以这样做.创建一个类:
But you could also do like this. Create a class:
public class YesterdayBean{
public String getYesterday(){
Calendar cal = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
cal.add(Calendar.DATE, -1);
return dateFormat.format(cal.getTime());
}
}
将它作为 bean 连接到您的 Camel(或 spring,如果您使用它)注册表.如果您不确定如何执行此操作,请查找 registry 和 bean.
Wire it to your Camel (or spring, if you use that) registry as a bean. If you are unsure how to do that, lookup registry and the "using" section of bean.
假设您在注册表中将 bean 命名为昨天",带弹簧:
Let's say you named the bean "yesterday" in the registry, with spring:
<bean id="yesterday" class="some.package.YesterdayBean"/>
然后将它与文件组件一起使用.
then just use it with the file component.
.to("file:fo/bar?fileName=${bean:yesterday}")
如果这只是您需要的一个地方,并且您正在使用 Java DSL,您也可以使用 Java 处理器预先创建日期并将其放在标题中.
If this is just one single place you need it, and you are using Java DSL, you could also just pre-create the date with a java processor and place it in a header.
像这样:
from("file:somewhere")
.process(new Processor(){
public void process(Exchange ex){
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
ex.getIn().setHeader("yesterday",cal.getTime());
}
})
.to("file:target?fileName=${date:header.yesterday:dd-MM-yyyy}");
}
这篇关于用 Camel 的表达语言获取和格式化昨天的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!