cancel
Showing results for 
Search instead for 
Did you mean: 

How to Subtract days or years from new java.text.SimpleDateFormat

SOLVED
Contributor

How to Subtract days or years from new java.text.SimpleDateFormat

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 an xml tag I'm populating the expression in.

 

Thank you,

Rob

2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
Occasional Contributor

Re: How to Subtract days or years from new java.text.SimpleDateFormat

These items I have used within the XML that I submit via SoapUI Pro and it works!
2016-02-02

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

 

Or make it a day in the past (6 days prior to today)

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

2016-02-02 12:18:34

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

2016-02-02T09:54:55.866-05:00 Time Zone

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

View solution in original post

Highlighted
Contributor

Re: How to Subtract days or years from new java.text.SimpleDateFormat

Hey - Yes I found something that works for me Groovy Wise.

I have a SETUP TestSuite that I pull data from Database and Groovy as a Config Data Source (to keep true automation).

 

 

 

//---Age Calculation Script
import groovy.time.TimeCategory

use (TimeCategory) 
{
   def ageInMonths6 = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date() -6.month)
      testRunner.testCase.testSuite.project.setPropertyValue( "ageInMonths6", ageInMonths6 )
def Age1 = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date() -12.month) testRunner.testCase.testSuite.project.setPropertyValue( "Age1", Age1 )
}

 

 

This way in the API I can use this in my Birth Date tag

 

${#Project#Age1}

Etc... I have a # of Ages that I need to test against, so I have more than just the 2 above.

 

The problem I had before was "Leap Year" and the xml string I had wasn't giving me the Exact Date

Rob

 

 

 

View solution in original post

5 REPLIES 5
Highlighted
Occasional Contributor

Re: How to Subtract days or years from new java.text.SimpleDateFormat

Did you ever resolve this? I am having the same issue. Running the new Date() -1 gives me 2016-02-32.
Highlighted
Occasional Contributor

Re: How to Subtract days or years from new java.text.SimpleDateFormat

These items I have used within the XML that I submit via SoapUI Pro and it works!
2016-02-02

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

 

Or make it a day in the past (6 days prior to today)

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

2016-02-02 12:18:34

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

2016-02-02T09:54:55.866-05:00 Time Zone

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

View solution in original post

Highlighted
Contributor

Re: How to Subtract days or years from new java.text.SimpleDateFormat

Hey - Yes I found something that works for me Groovy Wise.

I have a SETUP TestSuite that I pull data from Database and Groovy as a Config Data Source (to keep true automation).

 

 

 

//---Age Calculation Script
import groovy.time.TimeCategory

use (TimeCategory) 
{
   def ageInMonths6 = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date() -6.month)
      testRunner.testCase.testSuite.project.setPropertyValue( "ageInMonths6", ageInMonths6 )
def Age1 = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date() -12.month) testRunner.testCase.testSuite.project.setPropertyValue( "Age1", Age1 )
}

 

 

This way in the API I can use this in my Birth Date tag

 

${#Project#Age1}

Etc... I have a # of Ages that I need to test against, so I have more than just the 2 above.

 

The problem I had before was "Leap Year" and the xml string I had wasn't giving me the Exact Date

Rob

 

 

 

View solution in original post

Highlighted
Occasional Contributor

Re: How to Subtract days or years from new java.text.SimpleDateFormat

Thanks for the example. I have zero experience with Groovy right now so this helps to see something that works.
Highlighted
Contributor

Re: How to Subtract days or years from new java.text.SimpleDateFormat

Feel free to ping me, if I can help... I played with Groovy for 5 years now and got some tricks up my sleeve

New Here?
Join us and watch the welcome video:
Building with the Collaborator API

Help Documenation
The Collaborator API is a JSON-RPC API that lets you integrate with Collaborator 9.0 and above. For more information about how the API works, check out the API description here:


Sample Plugins
For an example of an add-in that uses the API, take a look at the Microsoft Word Add-in for Collaborator:


We list all the great plugins our network has already built on this page on the SmartBear website.

Description
SBC2020
Top Kudoed Authors