• 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
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

How to round off timestamp in SQL ResultSet  RSS feed

 
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: 2508
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • 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: 2508
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • 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
 
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!