Service Virtualization

  • 1.  Date format conversion & addition

    Posted Apr 07, 2017 06:42 AM

    Hello,

     

    I have the below requirement which I am unsure of how best to handle. Please suggest. I am using DevTest 9.5.1.

     

    Var1 date format = MM-YYYY (which I want to set to 1st day of the month, for example, if NOVEMBER 2014 then set to 2014-11-01)

    Var 2  date format = YYYY-MM-DD

    Once I convert var1 to var2 format then I have to do var2 + 5 years (in this case 2014-11-01 + 5 years)

    Var3 date format = DD-MMM-YYYY

    Again I need to convert (var2+5 years) to var3 format. That means 2019-11-01 should come as 01-NOV-2019



  • 2.  Re: Date format conversion & addition
    Best Answer

    Posted Apr 07, 2017 01:31 PM

    The snippet below does not convert through the intermediate var2 format. You could implement a JSR-223 and use Java Date and Calendar to do the same.  You might run the following untested code in ITR mode to see if it does what you want.

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Calendar;

    // set up date formats

    DateFormat originalFormat = new SimpleDateFormat("MM-yyyy");

    Date date = originalFormat.parse( "11-2014" );
    // or replace above using a property
    //Date date = originalFormat.parse( testExec.getStateValue( "propertyNameHere") );

     

    Calendar cal = Calendar.getInstance();
    cal.setTime( date );
    cal.add( Calendar.YEAR, 5 );     //add 5 yrs to date
    Date finalDate = cal.getTime();  //return the calculated date

     

    // save newly calculated date into a property after applying desired format

    DateFormat finalFormat    = new SimpleDateFormat( "dd-MMM-yyyy" );

    testExec.setStateValue( "fl_datePlus5", finalFormat.format( finalDate ) );

    // TODO: add return depending on Assertion or JSR step

     



  • 3.  Re: Date format conversion & addition

    Posted Apr 11, 2017 04:42 AM

    Thanks much. It works with small changes.