I am using the date in a path in Camel:
fileName=${date:now:dd-MM-yyyy}
but what I need is now - 1 day. Is that possible?
Expressions and Predicates can then be used to create the various Enterprise Integration Patterns in the DSL like with the Recipient List EIP. To support dynamic rules Camel supports pluggable Expression strategies using a variety of different Languages.
The simple language uses ${body} placeholders for complex expressions where the expression contains constant literals. The ${ } placeholders can be omitted if the expression is only the token itself. From Camel 2.5 onwards you can also use the alternative syntax which uses $simple{ } as placeholders.
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());
}
}
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.
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}")
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.
Like this:
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 Simple language date
variable supports command with offset:
Supported commands are:
now
for current timestamp, [...] Command accepts offsets such as:now-24h
orheader.xxx+1h
or evennow+1h30m-100
.
So you can write your assigment as:
fileName=${date:now-1d:dd-MM-yyyy}
Note that -1d
equals to -24h
even not mentioned in the documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With