我在Android中使用以下代码将“当前日期”时间保存为时间戳
userValues.put("rTime", ServerValue.TIMESTAMP);
现在我想将差异计算为
String posttime=1576917051506 //retrieve saved timestamp
String currenttime=ServerValue.TIMESTAMP //current time
difference=1hour 25 minutes
我该如何实现
答案 0 :(得分:1)
使用Joda时间:
{{1}}
答案 1 :(得分:0)
String posttime=1576917051506
String currenttime=ServerValue.TIMESTAMP
首先将时间转换为Long
long time1 = Long.valueof(posttime)
long time2 = Long.valueof(currenttime)
long diffrence = time2-time1
String myValue = convertSecondsToHMmSs(diffrence)
现在 myValue 是您的时间差异。就像1h:2m
public static String convertSecondsToHMmSs(long millis) {
long seconds = (millis / 1000) % 60;
long minutes = (millis / (1000 * 60)) % 60;
long hours = millis / (1000 * 60 * 60);
StringBuilder b = new StringBuilder();
b.append(hours == 0 ? "00" : hours < 10 ? String.valueOf("0" + hours) :
String.valueOf(hours));
b.append(":");
b.append(minutes == 0 ? "00" : minutes < 10 ? String.valueOf("0" + minutes) :
String.valueOf(minutes));
b.append(":");
b.append(seconds == 0 ? "00" : seconds < 10 ? String.valueOf("0" + seconds) :
String.valueOf(seconds));
return b.toString();
}