如何将上个星期一的上个月送到骡子?
我怎么能根据有效载荷获得上周一的上个月?
答案 0 :(得分:1)
以下DataWeave 1.0代码段应该可以完成这项工作:
%dw 1.0
// %var payload = |2018-01-01|
%function getLastDayOfLastMonth(date)(date - "P$(date.day)D" )
%function getWeekday(date)(date.dayOfWeek)
%function getWeekdayOfLastDayOfPreviousMonth(date)(getWeekday(getLastDayOfLastMonth(date)))
%output application/json
---
{
inputPayload : payload as :string {format : "yyyy-MM-dd"},
periodAsString: |P04D| as :string,
period: ("P$(payload.day)D") as :period,
lastDayOfPreviousMonth: getLastDayOfLastMonth(payload),
weekDayOfTheLastDayOfThePreviousMonth: getWeekdayOfLastDayOfPreviousMonth(payload),
lastMondayOfPreviousMonth: getLastDayOfLastMonth(payload) - "P$(getWeekdayOfLastDayOfPreviousMonth(payload) - 1)D",
lastSundayOfPreviousMonth: getLastDayOfLastMonth(payload) - "P$(getWeekdayOfLastDayOfPreviousMonth(payload))D"
}
输出:
{
"inputPayload": "2018-01-01",
"periodAsString": "P4D",
"period": "P1D",
"lastDayOfPreviousMonth": "2017-12-31",
"weekDayOfTheLastDayOfThePreviousMonth": 7,
"lastMondayOfPreviousMonth": "2017-12-25",
"lastSundayOfPreviousMonth": "2017-12-24"
}
我在输出中添加了一些其他字段,以便更容易理解,会发生什么。你真正需要的是输出的“lastMondayOfPreviousMonth”值。
请注意:
"P1D"
或"P${payload.day}D"
创建periods。另见:String interpolation。date - "P$(date.day)D"
答案 1 :(得分:-1)
在DataWeave中:
{ currentdateTime: (now as :localdatetime {format : "dd/MM/yyyy HH:mm:ss"})as :string {format : "dd/MM/yyyy HH:mm:ss"}, beforedate: ((currentdateTime) - |P1D|)as :string {format : "dd/MM/yyyy HH:mm:ss"} }
答案 2 :(得分:-1)
我认为最简单的方法是使用joda-time库来处理这个和类似的用例。以下是示例:
使用Mule 3.x和DataWeave 1.0
第1步:在项目pom.xml中包含joda-time依赖项
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>
第2步:创建一个java类和一个静态函数来获取上个月的上个星期一。您可以根据需要在此类中添加更多方法。
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();
}
}
第3步:在mule xml文件中创建一个全局函数
<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>
第4步:在DataWeave中调用此全局函数
<dw:transform-message>
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
lastMondayOfPreviousMonth: getLastMondayOfPreviousMonth()
}
]]></dw:set-payload>
</dw:transform-message>
使用Mule 4.x和DataWeave 2.0
第1步:在项目pom.xml中包含joda-time依赖项
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>
第2步:创建一个java类和一个静态函数来获取上个月的上个星期一。您可以根据需要在此类中添加更多方法。
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();
}
}
第3步:在DataWeave中调用此java静态方法(在这种情况下无需创建全局函数)
<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>