0

Before thinking this is a duplicate please read.

I have been reading through the posts here and elsewhere but cannot figure out why the date is coming out invalid in the first place.

I am using SoapUI Pro to make calls and have placed in the XML that submits some code reference in order to create dates in the past but I need them to be relative to today not something that will be in the future or distant past so I have used the following

${=import java.text.SimpleDateFormat;
new SimpleDateFormat("YYYY-MM-DD").format(new Date()-6);}

When I submit my call to the Web Service I get back this error.

'2016-02-32' is not a valid value for 'date'

Can someone please explain why this is occurring? and what am I needing to correct this?

Ultimately I need to be able to do two things.

  1. Create a date in the format of YYYY-MM-DD 6 days in the past
  2. Create a date time in the format of YYYY-MM-DD HH:mm:ss.SSSXXX

Greatly appreciate some assistance on this. Am I not to use the Date? I have seen some that are using the Calendar and new references to Java 8, it is just a time crunch thing this morning and I did not want to go down any rabbit holes to track possible things that may be an issue as the servers are not using Java 8 at this time.

edjm
  • 4,830
  • 7
  • 36
  • 65
  • I had the wrong format specified. See below for the link to the helpful guide that shows the correct date format syntax to use. – edjm Feb 01 '16 at 17:47

4 Answers4

6

Try something like this

${=import java.text.SimpleDateFormat; Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -6); new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());}

you can use it as well for the date and time, just replace the formatting string.

The corect pattern for formatting date is yyyy-MM-dd (see SimpleDateFormat javadoc)

  • y - means the current year
  • Y - means week year, can be different then year for the first and last week in year (explained difference)
  • d - day in month
  • D - day in year

Answer based on How to subtract X day from a Date object in Java?

Community
  • 1
  • 1
MJar
  • 741
  • 9
  • 26
  • I have been working this the 40 minutes. Trying to find what is going on in the service. Even using the above without the subtraction of days ${=import java.text.SimpleDateFormat; Calendar cal = Calendar.getInstance(); new SimpleDateFormat("YYYY-MM-DD").format(cal.getTime());} the server keeps responding with '2016-02-32' is not a valid value for 'date'. If I just HARD CODE the date of 2016-02-01 then it will go through just fine. What gives? – edjm Feb 01 '16 at 15:10
  • I tested it locally with SoapUI non pro version 5.0.0, with a mocked service inside the SoapUI and it was working just fine. Maybe there is some issue with: your SoupUI version or maybe you have set as default some non Gregorian calendar? Try to set a mock service within SoapUI and verify if the generated request is as expected. If the request is correct then maybe there is an issue on server side? – MJar Feb 01 '16 at 16:01
  • The thing I have not noticed before is that the date formatting pattern is incorrect. Should be 'yyyy-MM-dd'. – MJar Feb 01 '16 at 16:07
  • Thank you MJar, I'll give that a try once I get back to my office. – edjm Feb 01 '16 at 17:41
  • Excellent catch. I did not see that at all. It just happened to be working fine this past month because all of the days were indeed valid. 32 is not a valid day of a month. Thank you so very much for your help. I'm sure this will correct the issue I'm having in SoapUI. – edjm Feb 01 '16 at 17:46
1

This works for me in SoapUI v5.3.0:

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

MarkoD
  • 21
  • 3
0

Thanks again @MJar. These are formats I have used within the XML that I submit via SoapUI Pro and work!

Note: The code is all on a single line just because that is how I did it to put in the XML and not have multiple lines.

2016-02-02

${=import java.text.SimpleDateFormat; Calendar cal = Calendar.getInstance(); new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());}

2016-02-03T09:54:55.866-05:00

${=import java.text.SimpleDateFormat; Calendar cal = Calendar.getInstance(); new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").format(cal.getTime());}

Or make it a day in the past (this case 6 days ago)

${=import java.text.SimpleDateFormat; Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -6); new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());}
edjm
  • 4,830
  • 7
  • 36
  • 65
0

${=def now=new Date();now.format("yyyy-MM-dd")} And if you need it to be yesterday or a future date, just as +x to Date() example ${=def now=new Date()+10;now.format("yyyy-MM-dd")}