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
我试图将一个长值(从1970年1月1日经过的毫秒数)转换为格式为h的时间:m:s:ms我用作时间戳的长值,我从记录事件的字段时间戳得到来自log4j。我该如何进行转换?例如,为了得到分钟我尝试了以下所有失败:logEvent.timeStamp /(1000 * 60 * 60)和TimeUnit.MILLISECONDS.toMinutes(logEvent.timeStamp)但我得到了垃圾:我得到了
1289375173771 for logEvent.timeStamp
358159 for logEvent.timeStamp/ (1000*60*60)
21489586 for TimeUnit.MILLISECONDS.toMinutes(logEvent.timeStamp)
How can I convert this?
我该怎么转换呢?
Thanks
谢谢
151
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.
有关该类接受的其他格式字符串的说明,请参阅SimpleDateFormat。
See runnable example using input of 1200 ms.
请参阅使用1200 ms输入的runnable示例。
105
long millis = durationInMillis % 1000;
long secOnd= (durationInMillis / 1000) % 60;
long minute = (durationInMillis / (1000 * 60)) % 60;
long hour = (durationInMillis / (1000 * 60 * 60)) % 24;
String time = String.format("%02d:%02d:%02d.%d", hour, minute, second, millis);
26
I'll show you three ways to (a) get the minute field from a long value, and (b) print it using the Date format you want. One uses java.util.Calendar, another uses Joda-Time, and the last uses the java.time framework built into Java 8 and later.
我将向您展示三种方法(a)从长值获取分钟字段,以及(b)使用您想要的日期格式打印它。一个使用java.util.Calendar,另一个使用Joda-Time,最后一个使用Java 8及更高版本中内置的java.time框架。
The java.time framework supplants the old bundled date-time classes, and is inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project.
java.time框架取代了旧的捆绑日期时间类,并受到JSR 310定义的Joda-Time的启发,并由ThreeTen-Extra项目扩展。
The java.time framework is the way to go when using Java 8 and later. Otherwise, such as Android, use Joda-Time. The java.util.Date/.Calendar classes are notoriously troublesome and should be avoided.
使用Java 8及更高版本时,java.time框架是可行的方法。否则,如Android,请使用Joda-Time。 java.util.Date / .Calendar类是众所周知的麻烦,应该避免。
final long timestamp = new Date().getTime();
// with java.util.Date/Calendar api
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timestamp);
// here's how to get the minutes
final int minutes = cal.get(Calendar.MINUTE);
// and here's how to get the String representation
final String timeString =
new SimpleDateFormat("HH:mm:ss:SSS").format(cal.getTime());
System.out.println(minutes);
System.out.println(timeString);
// with JodaTime 2.4
final DateTime dt = new DateTime(timestamp);
// here's how to get the minutes
final int minutes2 = dt.getMinuteOfHour();
// and here's how to get the String representation
final String timeString2 = dt.toString("HH:mm:ss:SSS");
System.out.println(minutes2);
System.out.println(timeString2);
Output:
输出:
java.time24
09:24:10:254
24
09:24:10:25424 09:24:10:254 24 09:24:10:254
long millisecOndsSinceEpoch= 1289375173771L;
Instant instant = Instant.ofEpochMilli ( millisecondsSinceEpoch );
ZonedDateTime zdt = ZonedDateTime.ofInstant ( instant , ZoneOffset.UTC );
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "HH:mm:ss:SSS" );
String output = formatter.format ( zdt );
System.out.println ( "millisecondsSinceEpoch: " + millisecondsSinceEpoch + " instant: " + instant + " output: " + output );
millisecondsSinceEpoch: 1289375173771 instant: 2010-11-10T07:46:13.771Z output: 07:46:13:771
millisecondsSinceEpoch:1289375173771 instant:2010-11-10T07:46:13.771Z输出:07:46:13:771
14
It is possible to use apache commons (commons-lang3) and its DurationFormatUtils class.
可以使用apache commons(commons-lang3)及其DurationFormatUtils类。
org.apache.commons
commons-lang3
3.1
For example:
例如:
String formattedDuration = DurationFormatUtils.formatDurationHMS(12313152);
// formattedDuration value is "3:25:13.152"
String otherFormattedDuration = DurationFormatUtils.formatDuration(12313152, DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN);
// otherFormattedDuration value is "P0000Y0M0DT3H25M13.152S"
Hope it can help ...
希望它可以帮助......
8
long secOnd= TimeUnit.MILLISECONDS.toSeconds(millis);
long minute = TimeUnit.MILLISECONDS.toMinutes(millis);
long hour = TimeUnit.MILLISECONDS.toHours(millis);
millis -= TimeUnit.SECONDS.toMillis(second);
return String.format("%02d:%02d:%02d:%d", hour, minute, second, millis);
5
public static String timeDifference(long timeDifference1) {
long timeDifference = timeDifference1/1000;
int h = (int) (timeDifference / (3600));
int m = (int) ((timeDifference - (h * 3600)) / 60);
int s = (int) (timeDifference - (h * 3600) - m * 60);
return String.format("%02d:%02d:%02d", h,m,s);
5
Try this:
尝试这个:
String sMillis = "10997195233";
double dMillis = 0;
int days = 0;
int hours = 0;
int minutes = 0;
int secOnds= 0;
int millis = 0;
String sTime;
try {
dMillis = Double.parseDouble(sMillis);
} catch (Exception e) {
System.out.println(e.getMessage());
}
secOnds= (int)(dMillis / 1000) % 60;
millis = (int)(dMillis % 1000);
if (seconds > 0) {
minutes = (int)(dMillis / 1000 / 60) % 60;
if (minutes > 0) {
hours = (int)(dMillis / 1000 / 60 / 60) % 24;
if (hours > 0) {
days = (int)(dMillis / 1000 / 60 / 60 / 24);
if (days > 0) {
sTime = days + " days " + hours + " hours " + minutes + " min " + seconds + " sec " + millis + " millisec";
} else {
sTime = hours + " hours " + minutes + " min " + seconds + " sec " + millis + " millisec";
}
} else {
sTime = minutes + " min " + seconds + " sec " + millis + " millisec";
}
} else {
sTime = seconds + " sec " + millis + " millisec";
}
} else {
sTime = dMillis + " millisec";
}
System.out.println("time: " + sTime);
4
Doing
干
logEvent.timeStamp / (1000*60*60)
will give you hours, not minutes. Try:
会给你几个小时,而不是几分钟。尝试:
logEvent.timeStamp / (1000*60)
and you will end up with the same answer as
你会得到同样的答案
TimeUnit.MILLISECONDS.toMinutes(logEvent.timeStamp)