Support Questions
Find answers, ask questions, and share your expertise
Announcements
Check out our newest addition to the community, the Cloudera Innovation Accelerator group hub.

Extract Recent Past Saturday

Explorer

Hello Guys,

How to Extract recent past Saturday based on today date and is there any expression language to get by update attribute 

1 ACCEPTED SOLUTION

Master Guru

@sachin_32 
You can accomplish by utilizing the "Advanced UI" capability found in the UpdateAttribute processor.

The advanced UI allows you to create Rules (think if these as an IF/Then capability).
So you would setup 3 rules:
1. If current date falls on Mon - Fri, do X
2. if current date falls on Sat, do nothing

3. if current date falls on Sun, do Y


Expression Language guide

Below you can see I have created 3 rules (Day1-5, Day6, and Day7)

MattWho_0-1646853075404.png

Once you create a Rule, you need to provide a Condition (This is your boolean if statement)

In this case I am using it to figure out what the current day of the week with 1= Monday and 7 = Sunday and seeing if the day of the week is prior to Sat or after Sat in the current week.  

If a rules condition (if statement) resolves to a boolean "true", then the configured "Actions" (then statement) are evaluated. 

For my "Day1-5" rule, I set:
Condition:

 

${now():format('u'):lt(6)}

 

Action:

 

${now():toNumber():minus(${now():format('u'):plus(1):multiply(86400000)}):toDate():format("EEE, dd MMM yyyy")}

 


For my "Day6" rule, I set:

Condition:

 

${now():format('u'):equals(6)}

 

Action:

 

${now():format('EEE, dd MMM yyyy')}

 

 

For my "Day7" rule, I set:

Condition:

 

${now():format('u'):gt(6)}

 

Action:

 

${now():toNumber():minus(86400000):toDate():format("EEE, dd MMM yyyy")}

 

 

About above:
- The "now()" function returns the current date.

- 86400000 is the number of milliseconds in 1 day.
- So first I get the current date and convert it to milliseconds using the "toNumber()" function.
- Then for Day1-5, I am subtracting based on current day of week a multiple of days worth of milliseconds.
- For Day6, I am doing nothing other than reformatting the current days date.
- For Day7, I am just subtracting one day or 86400000 milliseconds

No matter which rule is applied the final date format i choose to write to an attribute named "PreviousSaturday" on the FlowFile is formatted using java simple date format "EEE, dd MMM yyyy"
Example: "Sat, 05 Mar 2022"

If you found this response assisted with your query, please take a moment to login and click on "Accept as Solution" below this post.

Thank you,

Matt

View solution in original post

1 REPLY 1

Master Guru

@sachin_32 
You can accomplish by utilizing the "Advanced UI" capability found in the UpdateAttribute processor.

The advanced UI allows you to create Rules (think if these as an IF/Then capability).
So you would setup 3 rules:
1. If current date falls on Mon - Fri, do X
2. if current date falls on Sat, do nothing

3. if current date falls on Sun, do Y


Expression Language guide

Below you can see I have created 3 rules (Day1-5, Day6, and Day7)

MattWho_0-1646853075404.png

Once you create a Rule, you need to provide a Condition (This is your boolean if statement)

In this case I am using it to figure out what the current day of the week with 1= Monday and 7 = Sunday and seeing if the day of the week is prior to Sat or after Sat in the current week.  

If a rules condition (if statement) resolves to a boolean "true", then the configured "Actions" (then statement) are evaluated. 

For my "Day1-5" rule, I set:
Condition:

 

${now():format('u'):lt(6)}

 

Action:

 

${now():toNumber():minus(${now():format('u'):plus(1):multiply(86400000)}):toDate():format("EEE, dd MMM yyyy")}

 


For my "Day6" rule, I set:

Condition:

 

${now():format('u'):equals(6)}

 

Action:

 

${now():format('EEE, dd MMM yyyy')}

 

 

For my "Day7" rule, I set:

Condition:

 

${now():format('u'):gt(6)}

 

Action:

 

${now():toNumber():minus(86400000):toDate():format("EEE, dd MMM yyyy")}

 

 

About above:
- The "now()" function returns the current date.

- 86400000 is the number of milliseconds in 1 day.
- So first I get the current date and convert it to milliseconds using the "toNumber()" function.
- Then for Day1-5, I am subtracting based on current day of week a multiple of days worth of milliseconds.
- For Day6, I am doing nothing other than reformatting the current days date.
- For Day7, I am just subtracting one day or 86400000 milliseconds

No matter which rule is applied the final date format i choose to write to an attribute named "PreviousSaturday" on the FlowFile is formatted using java simple date format "EEE, dd MMM yyyy"
Example: "Sat, 05 Mar 2022"

If you found this response assisted with your query, please take a moment to login and click on "Accept as Solution" below this post.

Thank you,

Matt