Home > Back-end >  Insert the Java mysql date time is wrong
Insert the Java mysql date time is wrong

Time:11-01

Need to insert time to mysql, including mysql corresponding field properties for the Date, the Java code is as follows:
 
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Try {
Java. SQL. The Date the register=new Java, SQL, the Date (SDF. Parse (us) getregisterTime ()). The getTime ());
Java. SQL. Date lastlogin=new Java, SQL, the Date (SDF. Parse (us) getLastLogin ()). The getTime ());
System. The out. Println (lastlogin. ToString ());
Psts2. SetDate (5, register);
Psts2. SetDate (6, lastlogin);
} the catch (ParseException e) {
e.printStackTrace();
}

The time to insert is the 2017-06-21
System. The out. Println (lastlogin. ToString ()); The output is correct: the 2017-06-21
But after inserted into the database, become the 2017-06-20 time, less a day, register the corresponding time is less a day
Database time_zone is "+ 08:00, CST
Not the field into a Datetime
There is something wrong with the place?

CodePudding user response:

Their top, online, etc

CodePudding user response:

Continue to wait,,,,

CodePudding user response:

Feeling is time zone, you'd better is to time accurate to hours, see how many hours difference, so convenient and find out the reason,

CodePudding user response:

reference zc881124 reply: 3/f
feel time zone, you had better be to time accurate to hours, see how many hours difference, so convenient and find out the reason,

The time zone set up CST, + 08:00, also still won't do

CodePudding user response:

Your SQL date and util date transformation way has a problem, you can check of mutual transformation between the two is how, always feel you into too much trouble

CodePudding user response:

Time to convert util package

CodePudding user response:

Java. Util. Date

CodePudding user response:

Have a look, if this can solve your problem,

https://www.jianshu.com/p/ea7ef2d29940

CodePudding user response:

First converted into "yyyy - MM - dd" string, again into your "yyyy - MM - dd" time format,

CodePudding user response:

You are using mysql is 5. X version or 8 x version, 8 x version. URL Settings when set the time zone serverTimezone=UTC, this date will be every other day, I happened to be met with this problem. You change my serverTimezone values, such as serverTimezone=Asia/Shanghai; I use 8 x version of mysql are several problems when connecting to the database. Such as Driver, 5. X version of mysql Driver=". Com. Mysql. JDBC Driver ". However, 8 x version of mysql Driver=". Com. Mysql. Cj. JDBC Driver ". There are 5. X version of urls do not need to set what time zone, character set, SSL, a series of problems such as rewriteBatchedStatements. Feeling get trouble

CodePudding user response:

With simpleDateFormat classes, in the associated pattern

CodePudding user response:

This time can try jdk8 Java. Time. LocalDate

CodePudding user response:

Try changing the database format to timestamp,
Like this

CodePudding user response:

Just picture not been sent out

CodePudding user response:

This, and if it is to be inserted into the database, string line ah,,,

CodePudding user response:

You need to add time to emphasize for Java. The util. The Date, then transform, the util Date to the Date of Sql, wouldn't be wrong

CodePudding user response:

After the connection to the database to add & amp; Try serverTimezone=Asia/Shanghai

CodePudding user response:

The
cited the 17th floor intresting_ reply:
after the database connection and & amp; Try serverTimezone=Asia/Shanghai
time zone problem, a new version of mysql default UTC and we sent 13 hours

CodePudding user response:

Because of time zone problem in the connection string followed by serverTimezone=CTT

CodePudding user response:

The
refer to the original poster fp2001 response:
need to insert time to mysql, including mysql corresponding field properties for the Date, the Java code is as follows:

The time to insert is the 2017-06-21
System. The out. Println (lastlogin. ToString ()); The output is correct: the 2017-06-21
But after inserted into the database, become the 2017-06-20 time, less a day, register the corresponding time is less a day
Database time_zone is "+ 08:00, CST
Not the field into a Datetime
There is something wrong with the place?


Time zone problem is Java set for connection to the SQL

JDBC: mysql://localhost: 3306/database? ServerTimezone=Asia/Shanghai
  • Related