irrelevantUser
irrelevantUser

Reputation: 1322

Apache Nifi Expression Language - toDate formatting

I am trying to format a date string using the Apache Nifi expression language and the Replace Text processor(regex). Given a date string

date_str : "2018-12-05T11:44:39.717+01:00", 

I wish to convert this to:

correct_mod_date_str: "2018-12-05 10:44:39.717", 

(notice how the date is converted to UTC, and character 'T' replaced by a space.)

To do this, I am currently using:

toDate("yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX"):format("yyyy-MM-dd HH:mm:ss.SSS", '+00:00')

and this works perfectly.

However, when the date string has 6 digits in ms, rather than 3, things break:

another_date_str: "2018-12-05T11:44:39.717456+01:00"

is converted to:

incorrect_mod_date_str: "2018-12-05 10:56:36.456"

It seems the first 3 digits in the ms precision interferes with the conversion.

Appreciate inputs to resolve this - what am I missing?

Regards

Upvotes: 1

Views: 6943

Answers (2)

blumenkoehlchen
blumenkoehlchen

Reputation: 13

I ran into a similar issue with date time encoded in ISO 8601. The problem is, that the digits after the second are defined as fragment of a second, not milliseconds. See answer to related topic

Upvotes: 0

daggett
daggett

Reputation: 28564

seems that's a limitation in java.

according to java documentation there is no support of more then 3 milliseconds digits.

https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

the simplest way is to remove extra digits like this:

attr:replaceAll('(\.\d{3})\d*','$1'):toDate("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"):format("yyyy-MM-dd HH:mm:ss.SSS", '+00:00')

Upvotes: 5

Related Questions