Java converts Unix timestamps into specified format dates for your reference. The specific content is as follows
When obtaining data from the server, sometimes the time in the data obtained is in many cases the timestamp is similar to this. Of course, it is impossible for us to present this data to users in the form of a timestamp. Usually, it is necessary to process this timestamp in a series of processing to make it into the format we want and accustomed to browsing. So how do we process these timestamp format data? Each language and framework has its own method and method.
The following will be implemented in Java. Let’s just read the code without saying nonsense...
Method implementation
/** * Java converts Unix timestamps to a specified format date string* @param timestampString Timestamps such as: "1473048265"; * @param formats The format to be formatted default: "yyyy-MM-dd HH:mm:ss"; * * @return Return result: "2016-09-05 16:06:42"; */ public static String TimeStamp2Date(String timestampString, String formats) { if (TextUtils.isEmpty(formats)) formats = "yyyy-MM-dd HH:mm:ss"; Long timestamp = Long.parseLong(timestampString) * 1000; String date = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp)); return date; }Calling methods
TimeStamp2Date("1473048265", "yyyy-MM-dd HH:mm:ss");Return result
2016-09-05 16:06:42
Convert Java specified format date to Unix timestamp
/** * Convert date format string to timestamp* * @param dateStr String date* @param format For example: yyyy-MM-dd HH:mm:ss * * @return */ public static String Date2TimeStamp(String dateStr, String format) { try { SimpleDateFormat sdf = new SimpleDateFormat(format); return String.valueOf(sdf.parse(dateStr).getTime() / 1000); } catch (Exception e) { e.printStackTrace(); } return ""; }Get the current timestamp (accurate to seconds)
/** * Get the current timestamp (exactly to seconds) * * @return nowTimeStamp */ public static String getNowTimeStamp() { long time = System.currentTimeMillis(); String nowTimeStamp = String.valueOf(time / 1000); return nowTimeStamp; }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.