on 2022 Mar 25 5:08 PM
Hi everybody,
I'm trying to use camel Date expression to calculate dates:
toDate = ${date:now+1M:YYYY-MM-dd}
fromDate = ${date:now-1M:YYYY-MM-dd}
I tried these and it create 2 properties with same current date.
Is it working?
Camel Reference: https://camel.apache.org/components/3.15.x/languages/simple-language.html
(I'm trying to remove groovy date calculations)
Federico Bellizia
Request clarification before answering.
Hi Federico
This is a very interesting question! I had to do quite a bit of digging to find the answer, so thank you for asking it 😄
First off, the date offsets only support milliseconds, seconds, minutes and hours. You can add or subtract a day (= 24 hours), or a week ( = 7 * 24 hours = 168 hours) but not a month, since the number of hours depends on the month in question.
No, this does not seem to be mentioned in neither the Apache Camel Simple documentation nor the Cloud Integration documentation, and that's pretty annoying.
What's happening with your specific example is that the uppercase "M" is treated as a lowercase "m", i.e. as an offset in minutes. You can verify this by adding "HH:mm" to your date pattern.
I believe I've located the code that does the conversion from offsets to milliseconds (it's here), and it's fairly clear from it why this is happening:
That's not a bug, mind you, since the code doesn't support months as an offset.
So you can't achieve your goal using Apache Camel Simple. But even if you could, I'm not sure that you should. It looks to me like every time a Simple date expression is evaluated, a new Date object is created. This opens the door to a race condition, where the first property is created immediately before the month changes, and the second one is created immediately after. That would give an unexpected result.
To sum up: You need to create the properties in a Groovy script, and make sure to use the same point in time as the basis for both.
Have fun with CPI and thanks again for asking a really interesting question!
Regards,
Morten
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Morten
....impressive that there is no reference on this anywhere...I had the same issue and solution was found only in your reply (thumbs up!!!)
Other References
Camel Components / Language / Simple (for v4.4)
and even in SAP Help (camel-data-model-in-nutshell ) the documentation is on high level.
Thank you.
Thank you for very good answer, that's another missing feature.
INSESITIVE in that place tell a lot of low sensitivity to this problem.
I will return to use groovy script for +1 month on date.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Happy to help!
Regards,
Morten
User | Count |
---|---|
34 | |
22 | |
16 | |
8 | |
5 | |
5 | |
4 | |
4 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.