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; }