Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

How to round off timestamp in SQL ResultSet

 
Ranch Hand
Posts: 41
  • 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!
 
Sheriff
Posts: 3102
459
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus 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: 41
  • 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
Sheriff
Posts: 3102
459
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus 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
 
They weren't very bright, but they were very, very big. Ad contrast:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic