cancel
Showing results for 
Search instead for 
Did you mean: 

Date comparisons in different formats

SOLVED
Occasional Contributor

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!

4 ACCEPTED SOLUTIONS

Accepted Solutions
Contributor

Re: Date comparisons in different formats

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'") 
If my answer helped you out, please consider leaving kudos and/or accepting as a solution.
Have an awesome day!
Community Hero

Re: Date comparisons in different formats

@MoUddin's answer should do it. If you still didn't manage it though we'll help you further.


If my answer helped please click on the 'Accept as Solution' button.
Occasional Contributor

Re: Date comparisons in different formats

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!

Occasional Contributor

Re: Date comparisons in different formats

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

10 REPLIES 10
Community Hero

Re: Date comparisons in different formats

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?



If my answer helped please click on the 'Accept as Solution' button.
Frequent Contributor

Re: Date comparisons in different formats

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

 

 

Occasional Contributor

Re: Date comparisons in different formats

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.

Occasional Contributor

Re: Date comparisons in different formats

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.

Frequent Contributor

Re: Date comparisons in different formats

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

Contributor

Re: Date comparisons in different formats

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'") 
If my answer helped you out, please consider leaving kudos and/or accepting as a solution.
Have an awesome day!
Community Hero

Re: Date comparisons in different formats

@MoUddin's answer should do it. If you still didn't manage it though we'll help you further.


If my answer helped please click on the 'Accept as Solution' button.
Occasional Contributor

Re: Date comparisons in different formats

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!

Occasional Contributor

Re: Date comparisons in different formats

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

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