Apache Nifi表达语言-toDate格式 [英] Apache Nifi Expression Language - toDate formatting
问题描述
我正在尝试使用Apache Nifi表达式语言和替换文本"处理器(regex)格式化日期字符串.给定日期字符串
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",
我希望将其转换为:
correct_mod_date_str: "2018-12-05 10:44:39.717",
(请注意如何将日期转换为UTC,并用空格替换字符"T".)
(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')
这很完美.
但是,当日期字符串中的 ms 以6位而不是3位表示时,事情就中断了:
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"
转换为:
incorrect_mod_date_str: "2018-12-05 10:56:36.456"
ms 精度中的前3位数字似乎会干扰转换.
It seems the first 3 digits in the ms precision interferes with the conversion.
赞赏输入以解决此问题-我想念什么?
Appreciate inputs to resolve this - what am I missing?
致谢
推荐答案
似乎在Java中是一个限制.
seems that's a limitation in java.
根据Java文档,不支持超过3毫秒的数字.
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')
这篇关于Apache Nifi表达语言-toDate格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!