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
Solved! Go to Solution.
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:INFO:Sat 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
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:INFO:Sat 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
Format is required for before parsing the date string.
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 ?
thanks Rupert!!
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
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?
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.
Subject | Author | Latest Post |
---|---|---|