SELECT statement from MS SQL returns dates with loss of 2-4 days
up vote
1
down vote
favorite
I use MS SQL Express 2014 to store some data.
I have 3 different tables, each of them contains a DATE type field.
When I do a select statement to each table via JDBC, the ResultSet in some cases returns dates with loss of 2-4 days.
The code looks like:
ResultSet rs = preparedStatement.executeQuery();
while(rs.next())
java.sql.Date date = rs.getDate("DATE");
Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".
I can't understand the reason of the problem, because
in some cases "date" contains the exact data as field in DB,
and in other cases "date" is losing from 2 to 4 days comparing to DB field.
Thank you!
java sql-server date datetime
New contributor
add a comment |
up vote
1
down vote
favorite
I use MS SQL Express 2014 to store some data.
I have 3 different tables, each of them contains a DATE type field.
When I do a select statement to each table via JDBC, the ResultSet in some cases returns dates with loss of 2-4 days.
The code looks like:
ResultSet rs = preparedStatement.executeQuery();
while(rs.next())
java.sql.Date date = rs.getDate("DATE");
Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".
I can't understand the reason of the problem, because
in some cases "date" contains the exact data as field in DB,
and in other cases "date" is losing from 2 to 4 days comparing to DB field.
Thank you!
java sql-server date datetime
New contributor
3
Could you add example dates from your DB for both cases, along with the result on java side ?
– Arnaud
22 hours ago
1
Just check the timezone for the both DB
– SaviNuclear
22 hours ago
The dates where the loss happens, are they historic dates, say, more than 100 years ago? Does the loss always happen for dates in that range or only sometimes? Do you get the same loss if you tryLocalDate date = rs.getObject("DATE", LocalDate.class)
instead? (Thejava.sql.Date
class is outdated and has some design issues to it, so all things being equal I would recommend the latter approach.)
– Ole V.V.
22 hours ago
1
Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".
– ReemuS
22 hours ago
1
Welcome to Stack Overflow. This is vuja de (never seen it before). Please show your query and a few rows of sample data illustrating the problem. You can edit your question to do this.
– O. Jones
19 hours ago
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I use MS SQL Express 2014 to store some data.
I have 3 different tables, each of them contains a DATE type field.
When I do a select statement to each table via JDBC, the ResultSet in some cases returns dates with loss of 2-4 days.
The code looks like:
ResultSet rs = preparedStatement.executeQuery();
while(rs.next())
java.sql.Date date = rs.getDate("DATE");
Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".
I can't understand the reason of the problem, because
in some cases "date" contains the exact data as field in DB,
and in other cases "date" is losing from 2 to 4 days comparing to DB field.
Thank you!
java sql-server date datetime
New contributor
I use MS SQL Express 2014 to store some data.
I have 3 different tables, each of them contains a DATE type field.
When I do a select statement to each table via JDBC, the ResultSet in some cases returns dates with loss of 2-4 days.
The code looks like:
ResultSet rs = preparedStatement.executeQuery();
while(rs.next())
java.sql.Date date = rs.getDate("DATE");
Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".
I can't understand the reason of the problem, because
in some cases "date" contains the exact data as field in DB,
and in other cases "date" is losing from 2 to 4 days comparing to DB field.
Thank you!
java sql-server date datetime
java sql-server date datetime
New contributor
New contributor
edited 19 hours ago
New contributor
asked 22 hours ago
ReemuS
92
92
New contributor
New contributor
3
Could you add example dates from your DB for both cases, along with the result on java side ?
– Arnaud
22 hours ago
1
Just check the timezone for the both DB
– SaviNuclear
22 hours ago
The dates where the loss happens, are they historic dates, say, more than 100 years ago? Does the loss always happen for dates in that range or only sometimes? Do you get the same loss if you tryLocalDate date = rs.getObject("DATE", LocalDate.class)
instead? (Thejava.sql.Date
class is outdated and has some design issues to it, so all things being equal I would recommend the latter approach.)
– Ole V.V.
22 hours ago
1
Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".
– ReemuS
22 hours ago
1
Welcome to Stack Overflow. This is vuja de (never seen it before). Please show your query and a few rows of sample data illustrating the problem. You can edit your question to do this.
– O. Jones
19 hours ago
add a comment |
3
Could you add example dates from your DB for both cases, along with the result on java side ?
– Arnaud
22 hours ago
1
Just check the timezone for the both DB
– SaviNuclear
22 hours ago
The dates where the loss happens, are they historic dates, say, more than 100 years ago? Does the loss always happen for dates in that range or only sometimes? Do you get the same loss if you tryLocalDate date = rs.getObject("DATE", LocalDate.class)
instead? (Thejava.sql.Date
class is outdated and has some design issues to it, so all things being equal I would recommend the latter approach.)
– Ole V.V.
22 hours ago
1
Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".
– ReemuS
22 hours ago
1
Welcome to Stack Overflow. This is vuja de (never seen it before). Please show your query and a few rows of sample data illustrating the problem. You can edit your question to do this.
– O. Jones
19 hours ago
3
3
Could you add example dates from your DB for both cases, along with the result on java side ?
– Arnaud
22 hours ago
Could you add example dates from your DB for both cases, along with the result on java side ?
– Arnaud
22 hours ago
1
1
Just check the timezone for the both DB
– SaviNuclear
22 hours ago
Just check the timezone for the both DB
– SaviNuclear
22 hours ago
The dates where the loss happens, are they historic dates, say, more than 100 years ago? Does the loss always happen for dates in that range or only sometimes? Do you get the same loss if you try
LocalDate date = rs.getObject("DATE", LocalDate.class)
instead? (The java.sql.Date
class is outdated and has some design issues to it, so all things being equal I would recommend the latter approach.)– Ole V.V.
22 hours ago
The dates where the loss happens, are they historic dates, say, more than 100 years ago? Does the loss always happen for dates in that range or only sometimes? Do you get the same loss if you try
LocalDate date = rs.getObject("DATE", LocalDate.class)
instead? (The java.sql.Date
class is outdated and has some design issues to it, so all things being equal I would recommend the latter approach.)– Ole V.V.
22 hours ago
1
1
Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".
– ReemuS
22 hours ago
Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".
– ReemuS
22 hours ago
1
1
Welcome to Stack Overflow. This is vuja de (never seen it before). Please show your query and a few rows of sample data illustrating the problem. You can edit your question to do this.
– O. Jones
19 hours ago
Welcome to Stack Overflow. This is vuja de (never seen it before). Please show your query and a few rows of sample data illustrating the problem. You can edit your question to do this.
– O. Jones
19 hours ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
ReemuS is a new contributor. Be nice, and check out our Code of Conduct.
ReemuS is a new contributor. Be nice, and check out our Code of Conduct.
ReemuS is a new contributor. Be nice, and check out our Code of Conduct.
ReemuS is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53222221%2fselect-statement-from-ms-sql-returns-dates-with-loss-of-2-4-days%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
Could you add example dates from your DB for both cases, along with the result on java side ?
– Arnaud
22 hours ago
1
Just check the timezone for the both DB
– SaviNuclear
22 hours ago
The dates where the loss happens, are they historic dates, say, more than 100 years ago? Does the loss always happen for dates in that range or only sometimes? Do you get the same loss if you try
LocalDate date = rs.getObject("DATE", LocalDate.class)
instead? (Thejava.sql.Date
class is outdated and has some design issues to it, so all things being equal I would recommend the latter approach.)– Ole V.V.
22 hours ago
1
Dates in DB looks like: "1942-06-10", "1938-03-15", etc. The loss happens only sometimes, no matter how the date looks. On Java side they become "1942-06-08" or "1938-03-11".
– ReemuS
22 hours ago
1
Welcome to Stack Overflow. This is vuja de (never seen it before). Please show your query and a few rows of sample data illustrating the problem. You can edit your question to do this.
– O. Jones
19 hours ago