java转换毫秒到时间格式 [英] java convert milliseconds to time format
问题描述
我正在尝试将长值(从1970年1月1日开始经过的毫秒数)转换为格式 h的时间:m:s:ms
< br>
我用作时间戳的长值,来自log4j的记录事件的字段 timestamp
。
我该怎么办转换?
例如,为了获得分钟,我尝试了以下操作并且都失败了:
logEvent.timeStamp /(1000 * 60 * 60)
TimeUnit.MILLISECONDS.toMinutes(logEvent.timeStamp)
但是我得到了垃圾:
我得到了
I am trying to convert a long value (The number of milliseconds elapsed from 1/1/1970) to a time of format h:m:s:ms
The long value I use as timestamp, I get from the field timestamp
of logging event from log4j.
How do I do the conversion?
For example to get the minutes I tried the following and all fail:
logEvent.timeStamp/ (1000*60*60) and
TimeUnit.MILLISECONDS.toMinutes(logEvent.timeStamp)
but I get garbage:
I get
1289375173771 for logEvent.timeStamp
358159 for logEvent.timeStamp/ (1000*60*60)
21489586 for TimeUnit.MILLISECONDS.toMinutes(logEvent.timeStamp)
我该怎么转换呢?
谢谢
推荐答案
试试这个:
Date date = new Date(logEvent.timeSTamp);
DateFormat formatter = new SimpleDateFormat("HH:mm:ss.SSS");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateFormatted = formatter.format(date);
参见 SimpleDateFormat ,用于描述该类接受的其他格式字符串。
See SimpleDateFormat for a description of other format strings that the class accepts.
参见使用1200毫秒的输入可运行的示例。
See runnable example using input of 1200 ms.
这篇关于java转换毫秒到时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!