如何根据Nifi流中的给定年份范围获取每个月的第一个日期 [英] How to obtain first date of each month based on given year range in Nifi flow
问题描述
我想知道根据日期范围获取每个月的起始日期值的最佳方法是什么.
I would like to know what could be the best way to obtain the starting date values for each month based on the date range.
例如:如果我的年份范围为 2015-11-10 和 2018-01-15
(格式为 YYYY-mm-dd).然后我想提取以下日期:
For example: If I am given a year range of 2015-11-10 and 2018-01-15
(format YYYY-mm-dd). Then I would like to extract following dates:
2015-12-01
2016-01-01
.
.
2018-01-01
推荐答案
您可以尝试使用此流程在提供的日期范围内生成每个月的第一天.
You can try to use this flow for generating the first day of each month in the provided date range.
整体流程
第一步配置:开始
第 2 步配置:配置日期范围
通过此步骤提供开始和结束日期作为配置参数.
Provide the start and end dates as configuration parameters via this step.
第 3 步配置:生成月份的第一个日期
这里使用了一个 Groovy 脚本,下面提供
This uses a Groovy script, which is provided below
Groovy 脚本
flowFile = session.get();
if(!flowFile)
return;
DATE_FORMAT = 'yyyy-MM-dd';
startDate = Date.parse(DATE_FORMAT, flowFile.getAttribute("start_date"));
endDate = Date.parse(DATE_FORMAT, flowFile.getAttribute("end_date"));
allFirstDates = "";
Calendar calendar = Calendar.getInstance();
Set firstDaysOfMonths = new LinkedHashSet();
for (int i = 0; i <= endDate-startDate; i++) {
calendar.setTime(startDate.plus(i));
calendar.set(Calendar.DAY_OF_MONTH, 1);
firstDayOfMonth = calendar.getTime();
if (firstDayOfMonth.compareTo(startDate) >= 0) {
firstDaysOfMonths.add(calendar.getTime().format(DATE_FORMAT));
}
}
firstDaysOfMonths.each {
firstDayOfMonth -> allFirstDates = allFirstDates + firstDayOfMonth + "\n";
}
flowFile = session.putAttribute(flowFile,"all_first_dates", allFirstDates );
session.transfer(flowFile,REL_SUCCESS)
第四步配置:查看结果
运行输出:
当流程运行时,属性 all_first_dates
将填充日期范围内每个月的第一个日期.
When the flow is run, the attribute all_first_dates
will be populated with the first dates of each month in the date range.
这篇关于如何根据Nifi流中的给定年份范围获取每个月的第一个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!