Forum Discussion

karengibbs's avatar
karengibbs
Occasional Contributor
6 years ago
Solved

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 kn...
  • MoUddin's avatar
    6 years ago

    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
    6 years ago
    MoUddin's answer should do it. If you still didn't manage it though we'll help you further.
  • karengibbs's avatar
    karengibbs
    6 years ago

    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
    6 years ago

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