Home  >  Article  >  WeChat Applet  >  WeChat public account development--analyzing CreateTime

WeChat public account development--analyzing CreateTime

零下一度
零下一度Original
2017-05-27 13:46:173370browse

As can be seen from the WeChat public platform's message interface guide, each type of message definition contains the CreateTime parameter, which represents the creation time of the message. As shown in the figure below:

#The above figure 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 something similar to "yyyy-MM-dd" that we are all familiar with. HH:mm:ss" standard format time. 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

Message creation time CreateTime defined in the message interface , it represents the number of seconds from 0:00:00 on January 1, 1970 to the time when the message was 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, first 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 indicates that the time is from 0 hour, 0 minute, 0 second and 0 millisecond on January 1, 1970Milliseconds, note that this is the number of milliseconds! So how to convert the long type time obtained here into a standard format time? The method is as follows:

/**
 * 演示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 from 1970, and System.currentTimeMillis() represents the number of milliseconds from 1970, the difference between them The conversion is equivalent to: 1 second = 1000 milliseconds. Multiply CreateTime by 1000 to get 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));
}

[Related recommendations]

1. WeChat public account platform source code download

2. Share the example tutorial of developing WeChat public account for credit card payment

3. Introduction to WeChat developmentCreateTime

The above is the detailed content of WeChat public account development--analyzing CreateTime. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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