Skip to content
Advertisement

calculating next date to the passed Date in WSO2 6.1.1 using script

I am trying to get next day of Date to the passed date.

Reading dates from registry file.

Registry look like this

splitting above content by using colon separator and consider this as startDate and EndDate. Using script mediator to add Next day of Date for both dates. because after process done, i need to update registry with Next Days Date.(not yet included here, only date conversion is included)

<?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>

Logs:

[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

Expected Output:

NextStartDate: 2020-12-02 NextEndDate: 2021-01-01

References: Date Conversion Link

Advertisement

Answer

You dont need to call new Date() since you have the date string already and Date object you have used is javascript object. Tryout this code. Dates are hardcoded here

<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>
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement