Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of CreateTime in the Development of Wechat official account

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The purpose of this article is to share with you the content of the sample analysis of CreateTime in the development of Wechat official account. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

As can be seen from the message interface guide of Wechat public platform, each type of message definition includes a CreateTime parameter, which indicates the creation time of the message, as shown in the following figure:

The figure above shows the definition of 4.1-text message in the message interface guide. Note the description of CreateTime: message creation time (integer). The point is that this is an integer time, not the standard format time similar to "yyyy-MM-dd HH:mm:ss" that we are all familiar with. The main purpose of this article is to introduce the meaning of the integer message creation time CreateTime defined in Wechat message interface, and how to convert CreateTime into the time format we are familiar with.

The meaning of integer CreateTime

The message creation time CreateTime defined in the message interface, which represents the number of seconds between January 1, 1970 and 00:00:00 when the message was created. Note that it is the number of seconds between, not milliseconds!

Conversion of integer CreateTime

In Java, we often get the time of long type in the following two ways, and code first:

/ * demonstrate two common ways to obtain long type time in Java * / public static void main (String [] args) {long longTime1 = System.currentTimeMillis (); / / 1373206143378 System.out.println (longTime1); long longTime2 = new java.util.Date (). GetTime (); / 1373206143381 System.out.println (longTime2);}

The above two methods for obtaining time of type long are equivalent, and the result obtained represents the number of milliseconds from 00:00:00 on January 1, 1970. Note that this is the number of milliseconds! So how does the long type of time obtained here be converted into standard format time? The methods are as follows:

/ * demonstrate two ways to obtain long type time commonly used in Java * / public static void main (String [] args) {/ / current time (milliseconds from 00:00:00 on January 1, 1970) long longTime = 1373206143378L; String stdFormatTime = formatTime (longTime); / / output: 2013-07-07 22:09:03 System.out.println (stdFormatTime) } / * convert long type time to standard format (yyyy-MM-dd HH:mm:ss) * * @ param longTime * @ return * / public static String formatTime (long longTime) {DateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); return format.format (new Date (longTime));}

The above demonstrates the time of converting a long type of time to a standard format, but simply uses the SimpleDateFormat class, which is easy to understand. So back to today's topic, the time of how to convert CreateTime to a standard format.

The CreateTime in the Wechat message interface represents the number of seconds away from 1970, while System.currentTimeMillis () represents the number of milliseconds away from 1970, and the conversion between them is equivalent to: 1 second = 1000 milliseconds, that is, CreateTime multiplied by 1000, becomes the number of milliseconds away from 1970, and you can use the above formatTime () method to deal with it, isn't it very simple?

Next, I'll just package a separate method that converts the message creation time of the integer in Wechat messages, CreateTime, into the standard format, as follows:

/ * time to convert CreateTime in Wechat message to standard format (yyyy-MM-dd HH:mm:ss) * * @ param createTime message creation time * @ return * / public static String formatTime (String createTime) {/ / convert the CreateTime passed in by Wechat to long type, multiplied by 1000 long msgCreateTime = Long.parseLong (createTime) * 1000L; DateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss") Return format.format (new Date (msgCreateTime));} Thank you for reading! This is the end of the article on "sample Analysis of CreateTime in the Development of Wechat official account". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report