-1

Application: SoapUI XML Resquest

I could swear this worked at one time where I use the below:

${=(new java.text.SimpleDateFormat("yyyy-MM-dd")).format( new Date() )}

To Subtract or Add I would add enclose the -# or +# like so:

 ${=${=(new java.text.SimpleDateFormat("yyyy-MM-dd")).format( new Date() )}-1

The result of the -1 is showing up as 1982

QUESTIONS:

  1. Why is it taking away the -MM-dd part?

  2. Why is it subtracting 23 years for -1

GOAL:

To be able to subtract from sysdate and the request show in yyyy-MM-dd format i.e. if I want someone to be 65 years old - i want to subtract from sysdate to get that.

Again this is a SoapUI tag I'm populating the expression in.

SiKing
  • 10,003
  • 10
  • 39
  • 90
Robert Long
  • 65
  • 1
  • 8

1 Answers1

2

You have the brackets misplaced! Let me break it down for you:

def yesterday = new Date() - 1
def sdf = new java.text.SimpleDateFormat("yyyy-MM-dd")
def yesterdayFormatted = sdf.format(yesterday)

If you want it in a SoapUI property one liner:

${=new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date() - 1)}

Note that you can achieve the exact same thing with (slightly more compact):

${=String.format('%tF', new Date() - 1)}

Docs for the formatter.

SiKing
  • 10,003
  • 10
  • 39
  • 90
  • Maybe you can help me because this is killing me this morning. Using SoapUI Pro and I have the following code in it. ${=import java.text.SimpleDateFormat; new SimpleDateFormat("YYYY-MM-DD").format(new Date()-6)} but what it is generating is '2016-02-32' is not a valid value for 'date' – edjm Feb 01 '16 at 13:40
  • @Elijah Do you have a new question? – SiKing Feb 01 '16 at 16:31
  • @SiKing I think I may have gotten the answer at a thread I started. http://stackoverflow.com/questions/35132272/how-to-get-in-soapui-the-date-minus-days/35132759?noredirect=1#comment57989941_35132759 I'll let you know as well. – edjm Feb 01 '16 at 17:43
  • @SiKing just an FYI that the issue is resolved and I had posted several examples of how to format the code. This was done for use in SoapUI Pro. – edjm Feb 03 '16 at 15:58