Solve the problem of inconsistent MySQL storage time

Solve the problem of inconsistent MySQL storage time

After obtaining the system time using Java and storing it in the MySQL database, when the time type is datetime or Timestamp, it is found that the database storage is inconsistent with the local time.

A big reason is that the time zone set for MySQL is inconsistent with your local time zone. The solution is to modify the MySQL time zone configuration, which you can find by searching online.

I recommend the second method

Just set it in the DBUtil class that connects to the database according to the actual needs of your project. For example, I changed the time zone to the same time zone as Shanghai, Asia in the following code.

private static String driver = "com.mysql.cj.jdbc.Driver";
    private static String url = "jdbc:mysql://127.0.0.1:3306/mypetstore?serverTimezone=Asia/Shanghai&useSSL=false";

serverTimezone=Asia/Shanghai is used to set the time zone.

When retrieving Timestamp data from MySQL and displaying it in the browser, there is always a .0 at the end, which is very annoying. What can I do?

You can use the following EL expression and JSTL tags to format it, where log.date is the time data found

<fmt:formatDate value="${log.date}" type="date" pattern="yyyy-MM-dd HH:mm:ss" />

Note: If you write it as yyyy-MM-dd hh:mm:ss, it will not distinguish between morning and afternoon

Supplement: Mysql storage time or date misalignment problem

Here is a discussion about the issue of the date being misplaced by one day or the time being incorrect when adding records to the MySQL database.

Problem scenarios

There is no problem with the code logic. When executing it step by step, I found that the date was correct when it was saved, but it was wrong when it was saved to the database.

reason

The reason is the problem with the parameters you added when configuring the data source connection. If I guess correctly, the serverTimezone parameter you configured should be UTC. This is because the wrong time zone is used, which causes the date to be misplaced when storing data.

insert image description here

Solution

Set serverTimezone to Asia/Shanghai, as shown below. Try again and the problem will be solved.

insert image description here

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Analyze the selection problem of storing time and date types in MySQL
  • Explanation of the problem of selecting MySQL storage time type
  • How to choose the right MySQL datetime type to store your time
  • MySQL statement to get all dates or months in a specified time period (without setting stored procedures or adding tables)
  • Best Practices Guide for Storing Dates in MySQL

<<:  Web designers should optimize web pages from three aspects

>>:  Analysis of the principles of docker containers

Recommend

In-depth study of MySQL composite index

A composite index (also called a joint index) is ...

How to make JavaScript sleep or wait

Table of contents Overview Checking setTimeout() ...

HTML table markup tutorial (6): dark border color attribute BORDERCOLORDARK

In a table, you can define the color of the lower...

How to solve the problem that Seata cannot use MySQL 8 version

Possible reasons: The main reason why Seata does ...

Detailed explanation of script debugging mechanism in bash

Run the script in debug mode You can run the enti...

How to set password for mysql version 5.6 on mac

MySQL can be set when it is installed, but it see...

Detailed explanation of Vue plugin

Summarize This article ends here. I hope it can b...

Detailed explanation of Vue.js directive custom instructions

Customize a demo command The syntax of Vue custom...

Specific steps to use vant framework in WeChat applet

Table of contents 1. Open the project directory o...

JavaScript Shorthand Tips

Table of contents 1. Merge arrays 2. Merge arrays...

How to install MySQL using yum on Centos7 and achieve remote connection

Centos7 uses yum to install MySQL and how to achi...

An example of how to implement an adaptive square using CSS

The traditional method is to write a square in a ...

Detailed explanation of Linux command file overwrite and file append

1. The difference between the command > and &g...