Forum Discussion

AClark's avatar
AClark
New Contributor
16 years ago

Properties Syntax for a DateTime string to be used in a Request.

I'm having a problem with the Properties syntax.
I'm trying to create a datatime string value like this: //YYDDDHHMMSSMMM (14 chars)
Here is c# code to do it.
        public string GenReferenceIdentifier()
        {
            DateTime dtNow = DateTime.Now;
            string strYear = dtNow.Year.ToString().Remove(0, 2);
            return (strYear.ToString().PadLeft(2, '0')
                    + dtNow.DayOfYear.ToString().PadLeft(3, '0')
                    + dtNow.Hour.ToString().PadLeft(2, '0')
                    + dtNow.Minute.ToString().PadLeft(2, '0')
                    + dtNow.Second.ToString().PadLeft(2, '0')
                    + dtNow.Millisecond.ToString().PadLeft(3, '0'));
        }
But how do I do this in soapUI ?
I tried something like this but I get errors.
${=(i= new java.util.Date();i.day;i.month;i.year;)}

This syntax works but I need a value from the current date time.
${=(int)(Math.random()*1000)}

I'm new to this tool so any helpful information about properties syntax would be great.
Thanks for any help.

6 Replies

  • AClark's avatar
    AClark
    New Contributor
    answered my own question.
    Here it is if any others need this info.
    ${= i=(new java.util.Date());def sdf = new java.text.SimpleDateFormat("yyMMddHHmmss");sdf.format(i)}
  • AClark's avatar
    AClark
    New Contributor
    Actually in my case the format should be:
    ${= i=(new java.util.Date());def sdf = new java.text.SimpleDateFormat("yyDHHmmssSSS");sdf.format(i)}
  • Hi,

    Another (slightly) shorter way of doing the same thing is:


    ${=new java.text.SimpleDateFormat("yyDHHmmssSSS").format(new java.util.Date())}


    Regards,
    Dain
    eviware.com
  • HaedenH's avatar
    HaedenH
    Occasional Contributor
    Hello,

    Thanks for posting the above code segments, I am currently using the following in my SoapUI Test Case to pull information from the server based on the current date/time:
    ${=new java.text.SimpleDateFormat("CCyyMMDDHHmm").format(new java.util.Date())}


    And for the most part, this works. However, the server is set to UTC where as the query defaults to EST time. So this query actually returns 5 hours worth of data. Ideally, I would like to set it so that the query only returns about 5 minutes worth of data, and also accounts for the 5 hour difference.

    I was able to throw together the following Java code to handle the hour and minute changes I desire
     public static void main(String[] args) {
    Date date = new Date();
    Calendar cal = (Calendar.getInstance());

    cal.add(Calendar.HOUR_OF_DAY, 5);
    cal.add(Calendar.MINUTE, -5);

    System.out.println("Date = " + date);
    System.out.println("Calendar = " + cal.getTime());

    }

    which outputs the date/time as follows:
    Date = Thu Feb 03 09:39:17 EST 2011
    Calendar = Thu Feb 03 14:34:17 EST 2011

    What I would like to know is if there is a way to incorporate this functionality into my soap statement? I'm new to programming in Soap, and I initially thought I could declare a variable for such a thing, but a quick Google search suggests that variables can't be directly declared in a Soap script? Can anyone offer any insight?

    Thanks in advance,
    HaedenH
  • HaedenH's avatar
    HaedenH
    Occasional Contributor
    Here's what I've come up with so far:

    <lastmodDtg>${= def i=(new java.util.Calendar.getInstance());def sdf = new java.text.SimpleDateFormat("yyyyMMddHHmm");i.add(Calendar.HOUR_OF_DAY, 5);i.add(Calendar.MINUTE, -5);sdf.format(i.getTime())}</lastmodDtg>


    It doesn't work... Yet...
    But I feel like I'm very close. Can anyone offer any advice?
  • HaedenH's avatar
    HaedenH
    Occasional Contributor
    Ah ha! Got it!

    I had a little bit of outside help, but here is what we've come up with:
    ${=cal = java.util.Calendar.getInstance();
    cal.add(Calendar.MINUTE, -5); //subtract 5 mins
    dfUTC = new java.text.SimpleDateFormat("yyyyMMddHHmm");
    dfUTC.setTimeZone(java.util.TimeZone.getTimeZone("UTC"));
    dfUTC.format(cal.getTime());}


    This code will set the time to the UTC (also 'GMT') Time Zone, subtract the desired 5 minutes, and format the output as follows:
    201102041416 becomes 201102041911

    Hope this helps someone in the future.