Support Questions

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

Date with now() in a specific format in a specific timezone

avatar
New Contributor

In an UpdateAttribute processor I am trying to get the date of yesterday, at midnight in the CET timezone then have it transformed to a specific format in UTC.

What I did was : 
${now():minus(86400000):format('yyyy-MM-dd\'T\'00:00:00\'Z\'' , 'CET')} 
but this still gave me the time at midnight in UTC and not CET.

What I want basically is if today is 2024-06-11 I want to get : 2024-06-10T22:00:00Z

also, there is this issue with the CEST , If I specify CET is CEST not considered ? 
I just don't want to have to change my processor every 6 months a year and have something that works no matter what

2 REPLIES 2

avatar
Expert Contributor

Ideally, it should return the current date minus 24 hours and convert it to a specific time zone. Please check your server time.

saquibsk_3-1718099917158.png

 

CET: ${now():toNumber():minus(86400000):format('yyyy-MM-dd HH:mm:ss', 'CET')}

GMT: ${now():toNumber():minus(86400000):format('yyyy-MM-dd HH:mm:ss', 'GMT')}

Midnight CET: ${now():toNumber():minus(86400000):format('yyyy-MM-dd', 'CET'):append(' 00:00:00')}

Thanks

Shakib M.

avatar
Master Mentor

@ranie 

I see a couple issues with your NiFi Expression Language (NEL) statement:

  1. I see some formatting issues in your java simple formatter string: 'yyyy-MM-dd\'T\'00:00:00\'Z\'.   Your single and double quotes are not balanced.
  2. You are using the function "format ()" to change the timezone, but you could also use the "formatInstant()" function.  
  3. You are missing the "toNumber()" function to convert the date string to a number before trying to apply a mathematically computation to it.

The Now() function will return the date current system time as the NiFi service sees it.
example: my NiFi server uses UTC timezone:

MattWho_0-1718113511430.png 

The toNumber() function will provide the current date and time as a number of milliseconds since midnight Jan 1st, 1970 GMT.   This number will always be a GMT value.

The formatInstant() function will allow you to take a GMT time or a Java formatted date string and reformat it for a different timezone.

Taking above feedback into consideration, the following NEL statement should work for you.

 

${now():toNumber():minus(86400000):formatInstant("yyyy-MM-dd'T'HH:mm:ss 'Z'", "CET")}

 

Pay close attention to your use of single and double quotes.

Please help our community thrive. If you found any of the suggestions/solutions provided helped you with solving your issue or answering your question, please take a moment to login and click "Accept as Solution" on one or more of them that helped.

Thank you,
Matt