使用脚本计算wso2 6.1.1中传递日期的下一个日期

o7jaxewo  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(532)

我想把约会的第二天改成过去的日期。
从注册表文件读取日期。

使用冒号分隔符拆分上述内容,并将其视为startdate和enddate。使用脚本中介为两个日期添加日期的第二天。因为在完成这个过程之后,我需要用下一天的日期更新注册表。(这里还不包括,只包括日期转换)

<?xml version="1.0" encoding="UTF-8"?>
<api context="/testdateapi" name="TestAPI" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="GET">
        <inSequence>
            <log level="custom">
                <property name="==TestDateAPI" value="called=="/>
            </log>
            <!-- <script language="js">
                    var log = mc.getServiceLog();        
    log.info("===Logging inside Script Mediator====");
    var result=new Date('2020-12-03');
  result.setDate(result.getDate() + 1);
  log.info("modifiedDate::::" + result);  
    mc.setProperty("modifiedDate",result);
</script> 
     <respond/>  -->
      <property name="RegPath" scope="default" type="STRING" expression="get-property('registry','gov:/XeroConfig/PNLDT.txt')"/>
            <script language="js"><![CDATA[var log = mc.getServiceLog(); 
            log.info("Logging inside Script Mediator"); 
            var dates = mc.getProperty("RegPath").split(":"); 
            log.info("Reg Dates: " + dates);
              var startDate = dates[0];
              var endDate = dates[1];   
             var nextStartDate=addNextDay(startDate);
             mc.setProperty("nextStartDate", nextStartDate);
             //var nextEndDate=addNextDay(endDate);
            // mc.setProperty("nextEndDate", nextEndDate);
            log.info("startDate:  " + startDate);
            log.info("endDate:  " + endDate);
            function addNextDay(dateString) {
            var  simpleDateFormat = Packages.java.text.SimpleDateFormat;
            var fomatter = new simpleDateFormat("yyyy-MM-dd");
            var date = Packages.java.util.Date;
            var currentDate = fomatter.format(new Date(dateString));
            log.info("Current date : " + currentDate );

            var calendar = Packages.java.util.Calendar.getInstance();
            calendar.setTime(fomatter.parse(currentDate));
            var daysToAdd = mc.getProperty("days");
            calendar.add(Packages.java.util.Calendar.DATE, 1);
            var destDate = fomatter.format(calendar.getTime()); 
            log.info("Destination date : " + destDate );
            return destDate;
            }
            ]]></script>
            <log level="custom">
                <property expression="$ctx:nextStartDate" name="====nextStartDate==="/>
            </log>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

日志:

[2020-12-04 10:35:18,197] []  INFO - LogMediator ==TestDateAPI = called==
[2020-12-04 10:35:18,202] []  INFO - ScriptMessageContext Logging inside Script Mediator
[2020-12-04 10:35:18,202] []  INFO - ScriptMessageContext Reg Dates: [Ljava.lang.String;@352bb9c2
[2020-12-04 10:35:18,213] []  INFO - ScriptMessageContext Current date : 1970-01-01
[2020-12-04 10:35:18,214] []  INFO - ScriptMessageContext Destination date : 1970-01-02
[2020-12-04 10:35:18,214] []  INFO - ScriptMessageContext startDate:  2020-12-01
[2020-12-04 10:35:18,214] []  INFO - ScriptMessageContext endDate:  2020-12-31
[2020-12-04 10:35:18,214] []  INFO - LogMediator ====nextStartDate=== = 1970-01-02

预期产量:
下次开始日期:2020-12-02下次结束日期:2021-01-01
references:date conversion 链接

yiytaume

yiytaume1#

您不需要调用new date(),因为您已经有了日期字符串,并且您使用的日期对象是javascript对象。试用此代码。日期在这里是硬编码的

<script language="js"><![CDATA[var log = mc.getServiceLog(); 
            log.info("Logging inside Script Mediator"); 

            var startDate = '2020-12-01';
            var endDate = '2020-12-31';   
            var nextStartDate=addNextDay(startDate);
            mc.setProperty("nextStartDate", nextStartDate);
            var nextEndDate=addNextDay(endDate);
             mc.setProperty("nextEndDate", nextEndDate);
            log.info("startDate:  " + startDate);
            log.info("endDate:  " + endDate);
            function addNextDay(dateString) {
            var  simpleDateFormat = Packages.java.text.SimpleDateFormat;
            var fomatter = new simpleDateFormat("yyyy-MM-dd");
            var currentDate = dateString;

            var calendar = Packages.java.util.Calendar.getInstance();
            calendar.setTime(fomatter.parse(currentDate));
            var daysToAdd = mc.getProperty("days");
            calendar.add(Packages.java.util.Calendar.DATE, 1);
            var destDate = fomatter.format(calendar.getTime()); 
            log.info("Destination date : " + destDate );
            return destDate;
            }]]></script>

相关问题