java获取毫秒数 转字符,在Java中将以毫秒为单位的时间戳转换为字符串格式的时间...

I am trying to convert a long value (number of milliseconds elapsed from 1/1/1970 i.e. Epoch) to time of format h:m:s:ms.

The long value I use as timestamp, I get from the field timestamp of a logging event from log4j.

So far I've tried the following and it fails:

logEvent.timeStamp/ (1000*60*60)

TimeUnit.MILLISECONDS.toMinutes(logEvent.timeStamp)

but I get incorrect value:

1289375173771 for logEvent.timeStamp

358159 for logEvent.timeStamp/ (1000*60*60)

21489586 for TimeUnit.MILLISECONDS.toMinutes(logEvent.timeStamp)

How do I go about this?

解决方案

Try this:

Date date = new Date(logEvent.timeSTamp);

DateFormat formatter = new SimpleDateFormat("HH:mm:ss.SSS");

formatter.setTimeZone(TimeZone.getTimeZone("UTC"));

String dateFormatted = formatter.format(date);

See SimpleDateFormat for a description of other format strings that the class accepts.

See runnable example using input of 1200 ms.

你可能感兴趣的:(java获取毫秒数,转字符)