Apache Nifi 表达式语言 - toDate 格式 [英] Apache Nifi Expression Language - toDate formatting
问题描述
我正在尝试使用 Apache Nifi 表达式语言和替换文本处理器(正则表达式)格式化日期字符串.给定一个日期字符串
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屋!