【问题标题】:How to validate the Given date is not a Future date in mule 3如何验证给定日期不是 mule 3 中的未来日期
【发布时间】:2021-06-09 07:33:45
【问题描述】:

我的要求是验证给定日期不应该是未来日期。

例如today date is 01/01/2020

输入日期:

30/01/2020

输出:

Date should not be Future date.

【问题讨论】:

    标签: mule mule-component mulesoft mule-esb


    【解决方案1】:

    您也可以使用相等运算符将它们作为日期进行比较。如果您的日期有时间,请确保您为这些边缘情况将事物转换为公共时区。

    对于骡子 3:

    %dw 1.0
    %input payload application/json
    %output application/json
    %function isFutureDate(dt) (dt as :date) > (now as :date)
    
    %var dt = "30/01/2020" as :date { format: "dd/MM/yyyy" }
    ---
    isFuture: isFutureDate(dt)
    

    对于骡子 4:

    %dw 2.0
    output application/json
    fun isFutureDate(dateToTest: Date | DateTime) =
        (dateToTest as Date) > (now() as Date)
    
    var date = |2021-06-10|
    ---
    isFutureDate(date)
    

    【讨论】:

      【解决方案2】:

      1.通过#[server.dateTime]获取当前日期。

      2.设置Transform消息。

      "datevalidation": (payload.DateOfBirth as :date {format:"yyyy-MM-dd"}) < (flowVars.currentdate as :date {format:"yyyy-MM-dd"}
      

      3.添加验证组件isTure

      <validation:is-true config-ref="Validation_Configuration" expression="#[payload.datevalidation]" doc:name="Validation" message="date should not be Future."/>
      

      完整代码。

      <mule xmlns:validation="http://www.mulesoft.org/schema/mule/validation" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
          xmlns:spring="http://www.springframework.org/schema/beans" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
      http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
      http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
      http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
      http://www.mulesoft.org/schema/mule/validation http://www.mulesoft.org/schema/mule/validation/current/mule-validation.xsd">
          <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
          <validation:config name="Validation_Configuration" doc:name="Validation Configuration"/>
          <flow name="helloFlow">
              <http:listener config-ref="HTTP_Listener_Configuration" path="/hello" allowedMethods="GET" doc:name="HTTP"/>
              <set-variable variableName="currentdate" value="#[server.dateTime]" doc:name="current date Variable"/>
              <dw:transform-message doc:name="Transform Message">
                  <dw:set-payload><![CDATA[%dw 1.0
      %output application/java
      ---
      {
          "datevalidation": (payload.DateOfBirth as :date {format:"yyyy-MM-dd"}) < (flowVars.currentdate as :date {format:"yyyy-MM-dd"})
      }]]></dw:set-payload>
              </dw:transform-message>
              <validation:is-true config-ref="Validation_Configuration" expression="#[payload.datevalidation]" doc:name="Validation" message="date should not be Future."/>
              <logger level="INFO" doc:name="Logger"/>
          </flow>
      </mule>
      

      希望它的帮助。

      【讨论】:

        【解决方案3】:

        另一个建议

        %dw 2.0
        output application/json
        var inpDate = "08/06/2021" as Date {"format": "dd/MM/yyyy"}
        var today= now() as Date {"format": "dd/MM/yyyy"} 
        ---
        if (daysBetween(inpDate,today) > 0) "PastDate" else "FutureDate"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-20
          • 2010-12-11
          • 1970-01-01
          • 2020-01-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多