• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

How to round off timestamp in SQL ResultSet

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using JDBC to query an SQL Server DB in my acceptance tests.

In one of my tests, the query returns a timestamp in the ResultSet. The problem that I'm facing is that the timestamp will be different each time the test runs, so I can't use an AssertEquals to verify the timestamp is correct.

Instead, I'm using the below assert statement:



This is working fine if the timestamp has 3 milliseconds, but if there are 0 milliseconds, 1, 2, etc. then the test fails.

What changes should I make so that this assert statement would cater for all of the possible timestamps?

I simply need to make sure that a timestamp is being returned, rather than the actual value.


Thanks a lot for any help in advance!
 
Saloon Keeper
Posts: 2722
354
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark D Anthony wrote:This is working fine if the timestamp has 3 milliseconds, but if there are 0 milliseconds, 1, 2, etc. then the test fails.

What changes should I make so that this assert statement would cater for all of the possible timestamps?


Do you mean that the milliseconds portion of the timestamp could have between zero and three digits?:
    zero digits:   2018-11-16 13:55:23.
    one digits:   2018-11-16 13:55:23.4
    two digits:   2018-11-16 13:55:23.41
    three digits:   2018-11-16 13:55:23.415
 
Mark D Anthony
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Mark D Anthony wrote:This is working fine if the timestamp has 3 milliseconds, but if there are 0 milliseconds, 1, 2, etc. then the test fails.

What changes should I make so that this assert statement would cater for all of the possible timestamps?


Do you mean that the milliseconds portion of the timestamp could have between zero and three digits?:
    zero digits:   2018-11-16 13:55:23.
    one digits:   2018-11-16 13:55:23.4
    two digits:   2018-11-16 13:55:23.41
    three digits:   2018-11-16 13:55:23.415



Hi, thanks for responding.

Yep, that's what I meant.
 
Ron McLeod
Saloon Keeper
Posts: 2722
354
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could modify your regex to allow the last group to have between zero and three digits.  You should also escape the period, since it has a special meaning in regex expressions.

There are plenty of online sites to use to test and tune your regex expressions.  This one is quite good: Regular Expression Test Page
 
Are you okay? You look a little big. Maybe this tiny ad will help:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!