问题
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.
- Create a date in the format of YYYY-MM-DD 6 days in the past
- 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.
回答1:
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?
回答2:
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());}
回答3:
This works for me in SoapUI v5.3.0:
${=new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date() - 6)}
回答4:
${=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")}
来源:https://stackoverflow.com/questions/35132272/how-to-get-in-soapui-the-date-minus-days