← All articles
DataWeave · MuleSoft

MuleSoft DataWeave Use Case - Get Last Monday of Previous Month

DataWeave is well suited for many transformation problems, but some requirements are easier to express by combining DataWeave with an established Java library.

One example is calculating the last Monday of the previous month. The same approach can be adapted for other calendar-based rules. This article uses the Joda-Time library and shows how the integration differs between Mule 3.x/DataWeave 1.0 and Mule 4.x/DataWeave 2.0.

Mule 3.x with DataWeave 1.0

Step 1: Add the Joda-Time dependency

Include the dependency in the project's pom.xml:

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.9.9</version>
</dependency>
View original Gist ↗

Step 2: Create a Java utility method

Create a Java class with a static method that calculates the last Monday of the previous month. Additional date-related methods can be added to the same utility as needed.

package com.mycompany;


import java.util.Date;
import org.joda.time.LocalDate;


public class DateFunction {


public static Date getLastMondayOfPreviousMonth() {
    return new LocalDate().minusMonths(1).dayOfMonth().withMaximumValue().dayOfWeek().withMinimumValue()
            .toDateTimeAtStartOfDay().toDate();
    }


}
View original Gist ↗

Step 3: Expose the Java method as a global Mule function

<configuration doc:name="Configuration">
    <expression-language>
        <import class="com.mycompany.DateFunction"/>
        <global-functions>
            def getLastMondayOfPreviousMonth() {
                return com.mycompany.DateFunction.getLastMondayOfPreviousMonth();          
            }
        </global-functions>
    </expression-language>
</configuration>
View original Gist ↗

Step 4: Invoke the global function from DataWeave

<dw:transform-message>
    <dw:set-payload><![CDATA[%dw 1.0
        %output application/json
        ---
        {
            lastMondayOfPreviousMonth: getLastMondayOfPreviousMonth()
        }
    ]]></dw:set-payload>
</dw:transform-message>
View original Gist ↗

Mule 4.x with DataWeave 2.0

Step 1: Add the Joda-Time dependency

Use the same dependency in the project's pom.xml:

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.9.9</version>
</dependency>
View original Gist ↗

Step 2: Reuse the Java utility method

package com.mycompany;


import java.util.Date;
import org.joda.time.LocalDate;


public class DateFunction {


public static Date getLastMondayOfPreviousMonth() {
    return new LocalDate().minusMonths(1).dayOfMonth().withMaximumValue().dayOfWeek().withMinimumValue()
            .toDateTimeAtStartOfDay().toDate();
    }


}
View original Gist ↗

Step 3: Invoke the static Java method directly from DataWeave

Unlike the Mule 3.x approach above, Mule 4.x can invoke the Java static method from DataWeave without first defining a global function.

<ee:transform>
    <ee:message>
        <ee:set-payload><![CDATA[%dw 2.0
            import java!com::mycompany::DateFunction
            output application/json
            ---
            {
                lastMondayOfPreviousMonth: DateFunction::getLastMondayOfPreviousMonth()
            }
        ]]></ee:set-payload>
    </ee:message>
</ee:transform>
View original Gist ↗

Takeaway

DataWeave does not need to solve every problem in isolation. For specialized date calculations, combining DataWeave with a focused Java utility can keep the transformation readable while reusing established library behavior. The integration mechanism differs between Mule 3.x and Mule 4.x, but the underlying utility logic can remain reusable.