As can be seen from the message Interface Guide for the micro-credit public platform, each type of message definition contains a createtime parameter that represents the time the message was created, as shown in the following illustration:
The figure above is the definition of the 4.1-text message in the Message interface Guide. Note the description of Createtime: Message creation Time (integer), which is focused on the time of an integer, rather than the standard format time that we all know similar to "Yyyy-mm-dd HH:mm:ss". This article is mainly about the meaning of the integer message creation time Createtime defined in the micro-mail message interface, and how to convert Createtime to the time format we are familiar with.
The meaning of integral type Createtime
The message creation time Createtime defined in the message interface, which represents the number of seconds from January 1, 1970 0:0 0 seconds to the time the message was created, noting the number of seconds in the interval, not the number of milliseconds!
Conversion of integral type Createtime
In Java, we often get the long type of time in the following two ways, preceded by the code:
/**
* Demonstrates two common ways to get a 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 a long type of time are equivalent, and the obtained results indicate the time from January 1, 1970 0:0 0 seconds of 0 milliseconds, note that this is the number of milliseconds! So how do you get the long type of time to convert to a standard format? The method is as follows:
/**
* Demonstrates two common ways to get a long type time in Java */public
static void Main (string[] args) {
// Current time (from January 1, 1970 0:0 0 seconds, 0 milliseconds)
long longtime = 1373206143378L;
String Stdformattime = Formattime (longtime);
Output: 2013-07-07 22:09:03
System.out.println (stdformattime);
}
/**
* Converts a long type of time into a 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 to convert a long type of time to a standard format, simply by using the SimpleDateFormat class, which is more understood. Then back to today's topic, how to convert Createtime to a standard format for time.
The createtime in the micro-mail message interface represent the number of seconds from 1970, and System.currenttimemillis () represents the number of milliseconds from 1970, and the conversion between them is equivalent to: 1 seconds =1000 milliseconds, The Createtime multiplied by 1000, becomes the distance 1970 the millisecond number, can use above Formattime () method to handle, is not very simple?
Below, I am still separate encapsulates a method that converts the integer message creation time Createtime in a micro-message to a standard format, as follows:
/**
* Converts createtime in a micro-mail message to a standard format (YYYY-MM-DD HH:mm:ss)
* *
@param createtime Message creation time
* @return
Public
static String Formattime (String createtime) {
//converts incoming createtime of a micro-letter to a 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));
}