Dorbs
Dorbs

Reputation: 183

Using Groovy to capture the last day of the previous month

I'm using Groovy(1.5) in Boomi to capture the last day of the previous month, and I've been mixing some things I found via Google:

import java.util.Properties;
import java.io.InputStream;

for( int i = 0; i < dataContext.getDataCount(); i++ ) {
    InputStream is = dataContext.getStream(i);
    Properties props = dataContext.getProperties(i);

    dataContext.storeStream(is, props);
}

var dt = new Date(); 
dt.setDate(1);  
dt.setHours(-1); 

props.setProperty("document.dynamic.userdefined.StartDate",dt);

But I'm being met with the following error:

No signature of method: com.sun.script.groovy.GroovyScriptEngine.var() is applicable for argument types: (java.util.Date) values: {Thu Feb 25 16:09:33 UTC 2021} (in groovy script);
 Caused by: No signature of method: com.sun.script.groovy.GroovyScriptEngine.var() is applicable for argument types: (java.util.Date) values: {Thu Feb 25 16:09:33 UTC 2021}

Upvotes: 1

Views: 1537

Answers (2)

Jacob Jarick
Jacob Jarick

Reputation: 63

The below example shows how to get the last day of the previous month and format as:

  • Date
  • Day of month
  • Short name of the day
  • Full name of the day

.

The main trick is subtracting the Calendar.DAY_OF_MONTH from the current date which will always provide the last day of the previous month.

.

import java.text.SimpleDateFormat;
def date = new Date();

date = date.minus(date.getAt(Calendar.DAY_OF_MONTH));

def ddmmyyyyDateFormat = new SimpleDateFormat("dd-MM-yyyy")
def dayShortNameDateFormat = new SimpleDateFormat("E");
def dayFullNameDateFormat = new SimpleDateFormat("EEEE");

println "Date of last day of previous month: " + ddmmyyyyDateFormat.format(date);
println "Last day of previous month: " + date.getAt(Calendar.DAY_OF_MONTH);
println "Short name of day: " + dayShortNameDateFormat.format(date);
println "Full name of day: " + dayFullNameDateFormat.format(date);

Upvotes: 0

Amar
Amar

Reputation: 61

var is a JavaScript reserved keyword. Two ways to fix this error:

  1. Use JavaScript as Language:

    enter image description here

  2. Replace var with def:

    def dt = new Date();
    dt.setDate(1);
    dt.setHours(-1);
    

Upvotes: 0

Related Questions