Forum Discussion

ilovechiku's avatar
ilovechiku
Contributor
8 years ago
Solved

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

  • 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

     

11 Replies

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    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

     

    • ilovechiku's avatar
      ilovechiku
      Contributor

      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 ?

      • rupert_anderson's avatar
        rupert_anderson
        Valued Contributor

        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

  • nmrao's avatar
    nmrao
    Champion Level 3

    Format is required for before parsing the date string.

     

     

     

  • 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?

     

     

    • nmrao's avatar
      nmrao
      Champion Level 3
      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?
      • ilovechiku's avatar
        ilovechiku
        Contributor

        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. 

         

    • Ruksar's avatar
      Ruksar
      New Contributor

      Hi ilovechiku 


      ilovechiku wrote:

      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?

       

       


      You are getting the above output because you are using the wrong date format. MM/dd/yyyy is right way to use. Replace last line with below

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