I am trying to store 2019-07-27 in sql DB but it stores 2019-07-26. Can anyone help me in this. I am using Java.sql.Date here. I also tried with Java.util.Date and Java.sql.Timestamp, all gives same result.
-
4Please show your code - it's impossible to tell what the issue is otherwise. – Joe Clay Jul 24 '19 at 12:07
-
3Please share your code with us. We can't help without that. – Joshua Schlichting Jul 24 '19 at 12:08
-
1*Exactly* what data type is the column? And what database? – Basil Bourque Jul 24 '19 at 16:56
-
1I recommend you neither use `java.sql.Date`, `java.util.Date` nor `java.sql.Timestamp`. Those classes are poorly designed and long outdated. Use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 24 '19 at 17:51
3 Answers
Time zones, and wrong class
You failed to provide enough info for a definite answer.
But I suspect your issue is due to time zones. The java.sql.Date
class is a terrible hack, pretending to represent a date-only but actually hiding a time-of-day and time zone too. Never use this class.
You are using terrible date-time classes that were supplanted years ago with the adoption of JSR 310. Use only classes from the java.time package.
If you want to store a date-only, use a column with a data type akin to the SQL-standard type DATE
.
In Java, use LocalDate
with JDBC 4.2 or later. This class has only a date, without time-of-day and without time zone.
LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;
To write to the database:
LocalDate ld = LocalDate.of( 2019 , 1 , 23 ) ;
…
myPreparedStatement.setObject( … , ld ) ;
To capture the current date, specify your desired/expected time zone. For any given moment, the date varies around the globe by time zone.
ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
LocalDate ld = LocalDate.now( z ) ;

- 303,325
- 100
- 852
- 1,154
Have a look at the time zone of your JVM and your database. And have a look at the documentation on the method(s) you use to create your java.sql.Date objects.
The JavaDoc for the constructor java.sql.Date(long date)
e.g. states (emphasis mine):
Constructs a Date object using the given milliseconds time value. If the given milliseconds value contains time information, the driver will set the time components to the time in the default time zone (the time zone of the Java virtual machine running the application) that corresponds to zero GMT.
In other words, if you set the time to midnight in your time zone, the constructor will set it to midnight GMT with the same date. But midnight GMT might be another day in your time zone.
That's the best answer I can give without seeing any code and any info about the db used...
Please add more info about the solution for the next person finding your question if my answer helped or give us some code if my answer doesn't lead you to a solution.
The answer on Question 14693148: JPA TemporalType.Date giving wrong date is a bit more elaborate and recommends using GMT/UTC everywhere.
The second answer on Question 21174634: JDBC ResultSet: I need a getDateTime, but there is only getDate and getTimeStamp recommends using the newer date/time classes when reading via JDBC. This might also be applicable to writing.

- 532
- 2
- 7
There are two chances :
- If your DB and APP server are in different time zones try Adjusting the time of the day based on the Time difference.
- This happens with LEAP years. In this case save the date with time as 12:00 instead of 0:00.
long hours = 12L * 60L * 60L * 1000L;
Date d = new Date(d1.getTime() + hours);

- 1,188
- 13
- 31