Apache Nifi Expression Language - toDate formatting

こ雲淡風輕ζ 提交于 2019-12-24 08:03:48

问题


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


回答1:


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')


来源:https://stackoverflow.com/questions/54150828/apache-nifi-expression-language-todate-formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!