Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

how get next Week Sunday in NIFI

avatar
New Contributor

Example : Input : 10/12/2019 
I need to get subsequent Sunday of given input date

1 ACCEPTED SOLUTION

avatar
Super Mentor

@bha 

This can be done via a single long complex NiFi Expression Language (EL) statement or via a couple smaller EL statements within the "Advanced" Ui of the UpdateAttribute processor.

 

Before you can use NiFI EL to solve this query, you will need to get your date in to a FlowFile attribute.  Since i have no idea where the date 10/12/2019 originates from, I can't help much here.  If it is on the content, perhaps extractText processor can be used.

 

Lets assume we have a FlowFile with the following FlowFile attribute on it:

   

FlowFile attribute name: FlowFile Attribute value:
mydate 10/12/2019

 

We can pass this FlowFile through an UpdateAttribute processor where we create a new attribute with the date of the subsequent Sunday. (My EL assumes that if input date is already Sunday, it will calculate the next Sunday instead of just reporting that today is Sunday)

 

Solution1 (one long NiFi EL statement):

Screen Shot 2019-10-14 at 11.12.22 AM.png

The full EL is below (all one line):

 

${mydate:toDate('MM/dd/yyyy'):format('u'):lt('7'):ifElse("${literal('7'):minus(${mydate:toDate('MM/dd/yyyy'):format('u')}):multiply('86400000'):plus(${mydate:toDate('MM/dd/yyyy'):toNumber()}):format('EEE,MM/dd/yyyy')}","${literal('604800000'):plus(${mydate:toDate('MM/dd/yyyy'):toNumber()}):format('EEE,MM/dd/yyyy')}")}

 

NOTE: Be careful with copy paste as it may screw up the single and double quote marks in the above EL making the EL statement invalid.

 

What above does is calculate day of week (1 - 7)...

IF - Then if that number is less then 7:

      --- it subtracts it from 7 to determine how many day until next Sunday remain.
      --- Then multiples that number by number of millseconds in a day
      --- Then adds that number to the millisecond value of your input date.  
      --- Then converts that new millisecond value back in to a date again (format here is Sun,10/13/2019)

ELSE - When number is not less then 7

      --- It adds 7 days in milliseconds to number to the millisecond value of your input date.

      --- Then converts that new millisecond value back in to a date again (format here is Sun,10/13/2019)

 

So essential you have tow EL statements handled by an ifElse function. One covers days 1 -6 and the other covers day 7.

 

Solution 2 (Using "Advanced" UI of UpdateAttribute to break apart above long EL):

The Advanced Ui allows you to construct rules.  Only the rules where the "conditions" resolve to "true" will apply the actions. So we simply create two rules.  One for days 1-6 and the other for when day of week is 7.

Day less than 7:

Screen Shot 2019-10-14 at 11.31.25 AM.png

Day equal 7:

Screen Shot 2019-10-14 at 11.32.03 AM.png

Using the "Advanced"UI makes handling complex EL statements easier.

 

Hope this helps,
Matt

 

View solution in original post

4 REPLIES 4

avatar
Super Mentor

@bha 

This can be done via a single long complex NiFi Expression Language (EL) statement or via a couple smaller EL statements within the "Advanced" Ui of the UpdateAttribute processor.

 

Before you can use NiFI EL to solve this query, you will need to get your date in to a FlowFile attribute.  Since i have no idea where the date 10/12/2019 originates from, I can't help much here.  If it is on the content, perhaps extractText processor can be used.

 

Lets assume we have a FlowFile with the following FlowFile attribute on it:

   

FlowFile attribute name: FlowFile Attribute value:
mydate 10/12/2019

 

We can pass this FlowFile through an UpdateAttribute processor where we create a new attribute with the date of the subsequent Sunday. (My EL assumes that if input date is already Sunday, it will calculate the next Sunday instead of just reporting that today is Sunday)

 

Solution1 (one long NiFi EL statement):

Screen Shot 2019-10-14 at 11.12.22 AM.png

The full EL is below (all one line):

 

${mydate:toDate('MM/dd/yyyy'):format('u'):lt('7'):ifElse("${literal('7'):minus(${mydate:toDate('MM/dd/yyyy'):format('u')}):multiply('86400000'):plus(${mydate:toDate('MM/dd/yyyy'):toNumber()}):format('EEE,MM/dd/yyyy')}","${literal('604800000'):plus(${mydate:toDate('MM/dd/yyyy'):toNumber()}):format('EEE,MM/dd/yyyy')}")}

 

NOTE: Be careful with copy paste as it may screw up the single and double quote marks in the above EL making the EL statement invalid.

 

What above does is calculate day of week (1 - 7)...

IF - Then if that number is less then 7:

      --- it subtracts it from 7 to determine how many day until next Sunday remain.
      --- Then multiples that number by number of millseconds in a day
      --- Then adds that number to the millisecond value of your input date.  
      --- Then converts that new millisecond value back in to a date again (format here is Sun,10/13/2019)

ELSE - When number is not less then 7

      --- It adds 7 days in milliseconds to number to the millisecond value of your input date.

      --- Then converts that new millisecond value back in to a date again (format here is Sun,10/13/2019)

 

So essential you have tow EL statements handled by an ifElse function. One covers days 1 -6 and the other covers day 7.

 

Solution 2 (Using "Advanced" UI of UpdateAttribute to break apart above long EL):

The Advanced Ui allows you to construct rules.  Only the rules where the "conditions" resolve to "true" will apply the actions. So we simply create two rules.  One for days 1-6 and the other for when day of week is 7.

Day less than 7:

Screen Shot 2019-10-14 at 11.31.25 AM.png

Day equal 7:

Screen Shot 2019-10-14 at 11.32.03 AM.png

Using the "Advanced"UI makes handling complex EL statements easier.

 

Hope this helps,
Matt

 

avatar
Contributor

the same way  i am calculating previous Saturday  but not able to get it can you help

--------------------------

${mydate:toDate('MM/dd/yyyy'):format('u'):lt('6'):ifElse("${literal('6'):minus(${mydate:toDate('MM/dd/yyyy'):format('u')}):multiply('86400000'):minus(${mydate:toDate('MM/dd/yyyy'):toNumber()}):format('EEE,MM/dd/yyyy')}","${literal('518400000'):minus(${mydate:toDate('MM/dd/yyyy'):toNumber()}):format('EEE,MM/dd/yyyy')}")}

avatar
Super Mentor

@sachin_32 

I would recommend utilizing the second option approach and make use of the UpdateAttribute advanced UI since you will need multiple rules.

You'll need a rule that handles when day of week is less than 6 and another rule for when day of week is greater than 6.  

Thanks,

Matt

avatar
Contributor

hi @MattWho ,

i checked with advance option also but wrong result is coming can you check this formula is it ok or not?

 

${mydate:toDate('MM/dd/yyyy'):format('u'):lt('6'):ifElse("${literal('6'):minus(${mydate:toDate('MM/dd/yyyy'):format('u')}):multiply('86400000'):minus(${mydate:toDate('MM/dd/yyyy'):toNumber()}):format('EEE,MM/dd/yyyy')}","${literal('518400000'):minus(${mydate:toDate('MM/dd/yyyy'):toNumber()}):format('EEE,MM/dd/yyyy')}")}