将毫秒格式设置为simpledate格式 [英] Format milliseconds to simpledate format

查看:52
本文介绍了将毫秒格式设置为simpledate格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当将毫秒格式设置为SimpleDate格式时,我面临一个奇怪的结果:

I'm facing a weird result when formatting milliseconds to a SimpleDate format:

输出为:

    Start date time: 11/06/30 09:45:48:970
    End date time: 11/06/30 09:45:52:831
    Execution time: 01:00:03:861

脚本:

    long dateTimeStart = System.currentTimeMillis();    
    // some script execution here
    long dateTimeEnd = System.currentTimeMillis();

    "Start date time: " + GlobalUtilities.getDate(dateTimeStart, "yy/MM/dd hh:mm:ss:SSS"); 
    "End date time: " + GlobalUtilities.getDate(dateTimeEnd, "yy/MM/dd hh:mm:ss:SSS"); 
    "Execution time: " + GlobalUtilities.getDate((dateTimeEnd - dateTimeStart), "hh:mm:ss:SSS");

方法:

    public static String getDate(long milliseconds, String format)
    {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(milliseconds);
    }

有人知道为什么执行时间值这么小吗?应该是00:00:03:861,而不是01:00:03:861

Any idea why the execution time value is so off? It should be 00:00:03:861, not 01:00:03:861

谢谢

推荐答案

执行时间关闭,因为Date构造函数花了很长时间指定从1970-01-01开始的毫秒数.

The execution time is off because the Date constructor takes a long specifying the number of milliseconds since 1970-01-01.

这篇关于将毫秒格式设置为simpledate格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆