Ask Your Question
1

Generate previous month days

asked 2019-06-20 04:06:57 -0500

Vss@2019 gravatar image

How to generate previous month days and store it in a field ?

For example : for previous month i have 31 days it has to generate date format like below

20190501
20190502


20190531
edit retag flag offensive close merge delete

Comments

Any update on this ?

Vss@2019 gravatar imageVss@2019 ( 2019-06-24 05:31:45 -0500 )edit

Can you clarify the question? I can't figure out exactly what it is you need. What is the input and output?

metadaddy gravatar imagemetadaddy ( 2019-06-24 13:01:17 -0500 )edit

I want to generate previous month all days by default. If i pass the parameter start_date and end_date it has to generate that many dates.

Vss@2019 gravatar imageVss@2019 ( 2019-06-25 04:48:27 -0500 )edit

Through which component we can generate it to achieve the expected output which is shown in previous comments

Vss@2019 gravatar imageVss@2019 ( 2019-06-25 04:49:54 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-06-26 23:52:02 -0500

metadaddy gravatar image

You can use one of the script evaluators to do this. Here is a solution in Groovy:

import java.time.LocalDate

for (record in records) {
  try {
    // Get today's date
    LocalDate now = LocalDate.now()
    // Get the last day of last month
    LocalDate lastMonth = now.minusDays(now.getDayOfMonth())

    // Get the year and month for last month
    int year = lastMonth.getYear()
    int month = lastMonth.getMonthValue()

    // Initialize list for the days from last month
    record.value['daysLastMonth'] = []
    // Loop through the days for last month, appending the string representation
    // to the list
    for (int day = 1; day < lastMonth.lengthOfMonth() + 1; day++) {
      record.value['daysLastMonth'].add(sprintf('%04d%02d%02d', year, month, day))
    }

    output.write(record)
  } catch (e) {
    log.error(e.toString(), e)
    error.write(record, e.toString())
  }
}
edit flag offensive delete link more

Comments

Thanks metadaddy

Vss@2019 gravatar imageVss@2019 ( 2019-06-27 06:40:43 -0500 )edit

@Vss@2109 Could you vote up and accept the answer? Thanks!

metadaddy gravatar imagemetadaddy ( 2019-06-27 09:36:11 -0500 )edit

I hope same thing works in jython

Vss@2019 gravatar imageVss@2019 ( 2019-06-27 10:41:18 -0500 )edit

I originally tried in Jython, but datetime doesn't play well with Jython. It still should be possible, though - see https://issues.streamsets.com/browse/SDC-3429 .

metadaddy gravatar imagemetadaddy ( 2019-06-27 10:45:14 -0500 )edit

Link does not exist.. can u guide me how to achieve in jython if there is any similar function for datetime

Vss@2019 gravatar imageVss@2019 ( 2019-06-27 10:48:05 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-06-20 04:06:57 -0500

Seen: 213 times

Last updated: Jun 26