forked from le-zw/nifi-sample-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetime.groovy
More file actions
36 lines (27 loc) · 1.21 KB
/
datetime.groovy
File metadata and controls
36 lines (27 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// date/times 操作
import org.apache.commons.io.IOUtils
import org.apache.nifi.processor.io.StreamCallback
import java.nio.charset.StandardCharsets
import java.text.SimpleDateFormat
def flowFile = session.get()
if (flowFile == null) {
return
}
try {
flowFile = session.write(flowFile, {inputStream, outputStream ->
SimpleDateFormat inputDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
TimeZone utcZone = TimeZone.getTimeZone("UTC")
inputDateFormat.setTimeZone(utcZone)
SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
TimeZone outputZone = TimeZone.getTimeZone("America/New_York")
outputDateFormat.setTimeZone(outputZone)
String inputDateTime = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
Date utcDate = inputDateFormat.parse(inputDateTime)
String outputDateTime = outputDateFormat.format(utcDate)
IOUtils.write(outputDateTime, outputStream, StandardCharsets.UTF_8)
} as StreamCallback)
session.transfer(flowFile, REL_SUCCESS)
} catch (Exception ex) {
flowFile = session.putAttribute(flowFile, "datetime.error", ex.getMessage())
session.transfer(flowFile, REL_FAILURE)
}