Home > Database > Mysql Tutorial > Suggestions for MySQL on saving time information

Suggestions for MySQL on saving time information

little bottle
Release: 2019-04-10 13:37:12
forward
2161 people have browsed it

I saw some netizens on the Internet saying that during the process of checking statistical data with SPs in India recently, they found that there was a time zone problem in the statistical data. This article mainly addresses the above issues. The interface he provides to the other party to obtain the statistical results of a specified date is controlled according to China's time zone. Fortunately, the international timestamp is stored in the database, so you only need to determine the time zone of the caller at the interface to obtain the correct statistical result information.

The solution reference is as follows, that is, based on the SimpleDateFormat object, it is necessary to clarify the current time zone that needs to be used.

SimpleDateFormat.setTimeZone(TimeZone);

// Java:
long timeMs = System.currentTimeMillis();
System.out.println("long = " + timeMs);

// current time zone
SimpleDateFormat default = new SimpleDateFormat("yyyy-MM-dd HH:mm");
System.out.println(default.format(timeMs));

// +8:00 time zone
SimpleDateFormat chinaFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
chinaFormat.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
System.out.println("GMT+8:00 = " + chinaFormat.format(timeMs));

// +5:30 time zone
SimpleDateFormat indiaFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
indiaFormat.setTimeZone(TimeZone.getTimeZone("GMT+5:30"));
System.out.println("GMT+5:30 = " + indiaFormat.format(timeMs));
Copy after login

Using the above method can actually solve the time zone problem, because I obtained the statistical data through the hour table and the hour The table has one record per hour. The Indian time zone is GMT 5:30. Based on the current table structure, there will be a half-hour data deviation problem. Therefore, in order to solve this problem, we modify the statistical granularity of the hour table to half an hour.

Summary:

This blog is just a simple solution to the key points of the time zone solution. The premise of using this solution is that the database stores international timestamps! ! !

Only time storage based on absolute timestamp, there is no time zone problem at all! ! ! Time zone is just a display issue! ! !

Make a little progress every day~

[Recommended course: MySQL video tutorial]

The above is the detailed content of Suggestions for MySQL on saving time information. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template