As can be seen from the message interface guide of the WeChat public platform, each type of message definition contains the CreateTime parameter, which represents the creation time of the message, as shown in the following figure:
The above picture is the definition of 4.1-Text Message in the Message Interface Guide. Pay attention to the description of CreateTime: message creation time (integer). The key 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. This article mainly wants to introduce the meaning of the integer message creation time CreateTime defined in the WeChat message interface, and how to convert CreateTime into a time format we are familiar with.
The meaning of integer CreateTime
CreateTime, the message creation time defined in the message interface, represents the number of seconds between 0:00:00 on January 1, 1970 and the time when the message is created. Note that it is the number of seconds of the interval, not the number of milliseconds!
Conversion of integer CreateTime
In Java, we often obtain long type time through the following two methods. Let’s start with the code:
/** * 演示Java中常用的获取long类型时间的两种方式 */ 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 of obtaining long type time are equivalent. The obtained result represents the number of milliseconds from 0:00:00:00 on January 1, 1970. Note that this is the number of milliseconds! So how to convert the long type time obtained here into a standard format time? Here’s how:
/** * 演示Java中常用的获取long类型时间的两种方式 */ public static void main(String[] args) { // 当前时间(距离1970年1月1日0时0分0秒0毫秒的毫秒数) long longTime = 1373206143378L; String stdFormatTime = formatTime(longTime); // 输出:2013-07-07 22:09:03 System.out.println(stdFormatTime); } /** * 将long类型的时间转换成标准格式(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 conversion of a long type time into a standard format time. It simply uses the SimpleDateFormat class, which is relatively easy to understand. So back to today’s topic, how to convert CreateTime into a standard format time.
CreateTime in the WeChat message interface represents the number of seconds since 1970, and System.currentTimeMillis() represents the number of milliseconds since 1970. The conversion between them is equivalent to: 1 second = 1000 milliseconds, that is, multiplying CreateTime by 1000 becomes the number of milliseconds since 1970. You can use the formatTime() method above to process it. Isn’t it very simple?
Next, I will encapsulate a separate method to convert the integer message creation time CreateTime in WeChat messages into a standard format time, as follows:
/** * 将微信消息中的CreateTime转换成标准格式的时间(yyyy-MM-dd HH:mm:ss) * * @param createTime 消息创建时间 * @return */ public static String formatTime(String createTime) { // 将微信传入的CreateTime转换成long类型,再乘以1000 long msgCreateTime = Long.parseLong(createTime) * 1000L; DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return format.format(new Date(msgCreateTime)); }