java 获取时间戳的三种方式

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

2022年8月15日09:42:53

java8生成时间戳

Instant timestamp = Instant.now();
Long millis = timestamp.toEpochMilli();

 

写在最后

  哪位大佬如若发现文章存在纰漏之处或需要补充更多内容,欢迎留言!!!

 相关推荐:

posted @ 2018-07-13 16:37  Marydon  阅读(9696)  评论(0编辑  收藏  举报