Forum Discussion

karengibbs's avatar
karengibbs
Occasional Contributor
7 years ago

Date comparisons in different formats

I am trying to compare dates sent to MySQL in ISO 8601 compliant format, i.e. "2018-12-31T12:59:59Z" that are retrieved from the dB column as "2018-12-31 12:59:59.0" using Groovy; however, I don't know how, or which date, I should convert to do the comparison.

 

Any suggestions would be greatly appreciated.

Thanks in advance!

  • One way you can achieve this is through java's simpleDateFormat:

     

     

    import java.text.SimpleDateFormat
    
    def sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'")
    def timeCheck = sdf.format(<java.util.Date object>)

    Documentation

     

     

    this sdf will put your Date object (30/07/2018 14:57:14.219) in the form 2018-07-30T14:57:14.219Z and this result is a string (you would pass the date object pulled from the db to the formatter)

     

    hopefully this is what you wanted,

    Mo

     

    EDIT: it seems you do not want the milliseconds, so your sdf should be 

    def sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") 
  • MoUddin's answer should do it. If you still didn't manage it though we'll help you further.
  • Not sure if  you received my thank you on the email feed, so wanted to thank you again here, Mo.

    Your suggestion was EXACTLY what I needed - thanks again!

  • Apologies if lots of duplicates are being posted - I am new to all this community/posting processes. Thanks, your suggestion worked for me!

  • One way you can achieve this is through java's simpleDateFormat:

     

     

    import java.text.SimpleDateFormat
    
    def sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'")
    def timeCheck = sdf.format(<java.util.Date object>)

    Documentation

     

     

    this sdf will put your Date object (30/07/2018 14:57:14.219) in the form 2018-07-30T14:57:14.219Z and this result is a string (you would pass the date object pulled from the db to the formatter)

     

    hopefully this is what you wanted,

    Mo

     

    EDIT: it seems you do not want the milliseconds, so your sdf should be 

    def sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") 
    • Lucian's avatar
      Lucian
      Community Hero
      MoUddin's answer should do it. If you still didn't manage it though we'll help you further.
    • karengibbs's avatar
      karengibbs
      Occasional Contributor

      Not sure if  you received my thank you on the email feed, so wanted to thank you again here, Mo.

      Your suggestion was EXACTLY what I needed - thanks again!

    • karengibbs's avatar
      karengibbs
      Occasional Contributor

      Apologies if lots of duplicates are being posted - I am new to all this community/posting processes. Thanks, your suggestion worked for me!

      • MoUddin's avatar
        MoUddin
        Contributor

        You're very welcome karen :smileyhappy:

        Glad I could be of help!


  • Lucian's avatar
    Lucian
    Community Hero

    What does it mean to compare the 2 dates? You just want to check if a date like "2018-12-31 12:59:59.0" is the same as a date like "2018-12-31T12:59:59Z"? Or do you want to also be able to tell which of them is before the other?

    • karengibbs's avatar
      karengibbs
      Occasional Contributor

      Your clarification question below is correct - I am trying to check if the date submitted is the same as the date that was pulled from the MySQL dB; so either I need to find out how to do the conversion the same way that was done for the dB resulting in the string retrieved so I can compare that they are the same ... OR I need to find out how to convert the retrieved string into the same format as was sent in the original request with the T and Z as part of the string.

  • jhanzeb1's avatar
    jhanzeb1
    Frequent Contributor

    Hi,

     

    When you send a date to a db, it does store it in it's own format unless you insert it as a string. 

    However, in your case, I would recommend you should store the date in column as a string so when you pull the date out of the column using groovy, you will see exactly what's gone in. 

     

    Could you elaborate in detail, what exactly it is that you are trying to achieve??

     

     

    • karengibbs's avatar
      karengibbs
      Occasional Contributor

      I have no control over how the date is inserted into the dB. As a tester for one of the features developed by an API development team, I need to confirm that the submitted date (which must be sent in the ISO 8601 Standard format with the T and Z as part of the string as described in my original question) is the same as the date and time that was inserted into the dB, however the date was modified into a string without the T and Z and my test assertion fails because it is not EXACTLY the same as what was sent. So I am trying to learn how to convert one or the other of the date strings so my compare is successful.

      • jhanzeb1's avatar
        jhanzeb1
        Frequent Contributor

        Hi, 

        In this case, I think you need to let API development team know that the date stored in the database is not in the right format and that they might need to store that date as a string rather than a date. As you said you have no control over how the date gets inserted into the database, that's the problem. The date should stay in the correct format while it's in the database. 

         

        Goodluck