加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

在Java中格式化*相对*时间(ms)?

发布时间:2020-12-15 08:45:43 所属栏目:Java 来源:网络整理
导读:我想根据相对时间记录某些事件.我不关心当天发生什么事,我只是想知道事件发生在2分13秒和243毫秒之前.比方说我有: long event_ms = [some time in ms since the Epoch];long now_ms = [current time in ms since the Epoch];long diff_ms = now_ms - event_
我想根据相对时间记录某些事件.我不关心当天发生什么事,我只是想知道事件发生在2分13秒和243毫秒之前.比方说我有:

long event_ms = [some time in ms since the Epoch];
long now_ms = [current time in ms since the Epoch];
long diff_ms = now_ms - event_ms;

diff_ms包含事件发生的AGO毫秒数.我只是想这次格式化(使用格式HH:mm:ss.SSS).最简单的方法是什么?谢谢!

解决方法

以下代码对我来说很好:

int diff_ms = 61200;
    SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss.SSS");
    fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
    String str = fmt.format(new Date(diff_ms));
    System.out.println(str);

它打印00:01:01.200

注意设置时区.否则结果取决于您的默认时区,除GMT外,其他地方都会出错.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读