java 获取时间戳的三种方式


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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM