java 獲取時間戳的三種方式
CreationTime--2018年7月13日16點29分
Author:Marydon
1.實現方式
方式一:推薦使用
System.currentTimeMillis()
方式二
new Date().getTime();
方式三
Calendar.getInstance().getTimeInMillis();
2.哪個最快?
方式一 > 方式二 > 方式三
2019/01/09
3.時間戳轉日期
/**
* 時間戳轉日期
* @explain
* @param timestamp 時間戳可以為String類型,也可以為long類型
* @return yyyy-MM-dd HH:mm:ss格式字符串
*/
public static String timestampToDate(Object timestamp) {
long sjc = 0;
String dateStr = "";
if (timestamp instanceof String)
sjc = Long.parseLong((String) timestamp);
else if (timestamp instanceof Long)
sjc = (Long) timestamp;
else
return dateStr;
Date date = new Date(sjc);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateStr = sdf.format(date);
return dateStr;
}
