cancel
Showing results for 
Search instead for 
Did you mean: 

Parsing a date in groovy.

SOLVED
Contributor

Parsing a date in groovy.

I have a response with a date just like this 

BirthDate": "1972-02-12T00:00:00"

 

i want to get to date like this 02/12/1972

 

How can i do that?.

So far I have tried

 

def bday =jsonResponse.Demographics.BirthDate
log.info "bday"+bday
def formatter = new SimpleDateFormat();
Date date = formatter.parse(bday);

 

am getting a error saying "unparseable date"

 

can anyone help

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Valued Contributor

Re: Parsing a date in groovy.

Hi,

 

Is this what you need?

Date date = Date.parse("yyyy-MM-dd'T'HH:mm:ss", bday)
log.info date

Wed May 11 13:52:49 BST 2016:INFOSmiley Frustratedat Feb 12 00:00:00 GMT 1972

You can also format it (to a different String format) if need be e.g.

 

log.info Date.parse("yyyy-MM-dd'T'HH:mm:ss", bday).format('dd/MM/yyyy')

Wed May 11 13:56:29 BST 2016:INFO:12/02/1972

Regards,

Rupert

 

Author of SoapUI Cookbook

View solution in original post

11 REPLIES 11
Highlighted
Valued Contributor

Re: Parsing a date in groovy.

Hi,

 

Is this what you need?

Date date = Date.parse("yyyy-MM-dd'T'HH:mm:ss", bday)
log.info date

Wed May 11 13:52:49 BST 2016:INFOSmiley Frustratedat Feb 12 00:00:00 GMT 1972

You can also format it (to a different String format) if need be e.g.

 

log.info Date.parse("yyyy-MM-dd'T'HH:mm:ss", bday).format('dd/MM/yyyy')

Wed May 11 13:56:29 BST 2016:INFO:12/02/1972

Regards,

Rupert

 

Author of SoapUI Cookbook

View solution in original post

Community Hero

Re: Parsing a date in groovy.

Format is required for before parsing the date string.

 

Selection_277.png

 

 



Regards,
Rao.
Contributor

Re: Parsing a date in groovy.

on doing the below

def bday =jsonResponse.Demographics.BirthDate

log.info "bday"+bday

def newDate= Date.parse("yyyy-MM-dd'T'HH:mm:ss", bday).format('MM/DD/YYYY')

log.info "dattttte"+newDate

Am getting the Wrong date like this

 

Wed May 11 08:52:50 CDT 2016:INFO:dattttte02/43/1972 not sure where the 43 is coming from ?

Contributor

Re: Parsing a date in groovy.

thanks Rupert!!

Valued Contributor

Re: Parsing a date in groovy.

Hi,

 

You need to consider that those patterns are case sensitive i.e. MM/DD/YYYY is not the same as what I put MM/dd/yyyy, use the lowercase versions of dd and yyyy for what you want I think.

 

Thanks,

Rupert

Author of SoapUI Cookbook
Community Hero

Re: Parsing a date in groovy.

Any reason for not using above proven solutions?


Regards,
Rao.
Contributor

Date Parse SOAP UI Issue?

Not sure why the behavior is so strange in Parsing..

 

I have code like this

//capturing date from response before date parse

def bday =jsonResponse.Demographics.BirthDate

 

log.info "Response date Before Date Parse "+bday

//Parsing the date from response above

def newDate= Date.parse("yyyy-MM-dd'T'HH:mm:ss", bday).format('MM/dd/YYYY')

 

log.info "Response Date"+newDate

 

Output :

Response date Before Date Parse 1971-12-28T00:00:00
Response Date12/28/1972 ---??? WHY IS THIS DATE ADDING A EXTRA year?

 

 

Community Hero

Re: Date Parse SOAP UI Issue?

Is it that you agreed to provided solution saying that is working? And despite being requested to look at the solutions, again creating same topic ? Do you care about people time or just ignore and go on your own way?


Regards,
Rao.
Contributor

Re: Date Parse SOAP UI Issue?

I think you are misunderstanding the question. I accepted the answer on Parsing the date and formatting. 

However I have seen a instance where on successful parsing its changing the year on the result,

 

So my second question is why is the results adding a year. Hence opened up a new question. 

 

New Here?
Join us and watch the welcome video:
Watch the new Interview
Top Kudoed Authors