Get and format yesterday's date in the Camel expression language

I use the date in the path in Camel:

fileName=${date:now:dd-MM-yyyy} 

but now I need 1 day. Is it possible?

+11
source share
4 answers

Well, not directly. Date: an object in a simple language can capture only the current time (or some time value that you placed inside the header), which you could do in java or similar.

But you can also do it. 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()); } } 

Connect it to your camel (or spring, if you use this) registry as a bean. If you do not know how to do this, find the registry and the "Use" section of the bean .

Let's say you called 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 the one place you need and you are using Java DSL, you can also just create a date using the java processor and put it in the 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}"); } 
+13
source

I was curious about this and tried some help from the camel mailing list. In fact, you can do what you request with built-in scripts like groovy. See here .

I got this to work for me:

 <camelContext id="contextname"> <route id="routename"> <from uri="file://\temp\?fileName=#myGroovyExp" /> <split> <tokenize token="(?=MSH\|)" regex="true" /> <to uri="bean:filePickupByDateTest?method=test" /> </split> </route> </camelContext> <spring:bean id="myGroovyExp" class="org.apache.camel.model.language.GroovyExpression"> <spring:constructor-arg index="0" value="new Date().previous().format('MMddyy') + 'pa'" /> </spring:bean> 

Where are my file names yesterday: MMddyypa

You just need to change the body of the script to:

 new Date().previous().format('dd-MM-yyyy') 

You need the groovy camel (or any other script lang you use) in your path, of course.

+3
source

Solution using file name filter:

Create a class that implements the org.apache.camel.component.file.GenericFileFilter file and implements the accept method to validate the file name.

 public class CustomFileName implements GenericFileFilter { public boolean accept(GenericFile file) { Calendar cal = Calendar.getInstance(); DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); cal.add(Calendar.DATE, -1); return file.getFileName().equals ("FILENAME_PREFIX"+dateFormat.format(cal.getTime()) + ".EXT"); } } 

In spring config

 <bean id="customFileFilter" class="com.sample.proj.util.CustomFileName"/> 

and

 <route> <description>Route for copying file from one location to another with custom file name filter</description> <from uri="file://C:\Source?filter=#customFileFilter" /> <to uri="file://C:\Destination" /> </route> 
0
source
 fileName=${date:now-24h:dd-MM-yyyy} 
0
source

All Articles