0 unix datetime android unix-timestamp kotlin
我有两个 UNIX 时间戳并且我正在使用 KOTLIN
1) 旧时间 - 1534854646 2) 当前时间 - 1534857527
现在我想要小时和分钟的差异。
val result = DateUtils.getRelativeTimeSpanString(1534854646, 1534857527, 0)
Run Code Online (Sandbox Code Playgroud)
但它给了我 2 秒,但实际时差约为 0 小时 48 分钟。
我也尝试过:
long mills = 1534857527 - 1534854646;
int hours = millis/(1000 * 60 * 60);
int mins = (mills/(1000*60)) % 60;
String diff = hours + ":" + mins;
Run Code Online (Sandbox Code Playgroud)
但它仍然给出 0 小时 0 分钟。
这是我的解决方案,代码是用 Kotlin 编写的。
时间以小时为单位.kt
class TimeInHours(val hours: Int, val minutes: Int, val seconds: Int) {
override fun toString(): String {
return String.format("%dh : %02dm : %02ds", hours, minutes, seconds)
}
}
Run Code Online (Sandbox Code Playgroud)
编写一个函数,将持续时间(以秒为单位)转换为TimeInHours。
fun convertFromDuration(timeInSeconds: Long): TimeInHours {
var time = timeInSeconds
val hours = time / 3600
time %= 3600
val minutes = time / 60
time %= 60
val seconds = time
return TimeInHours(hours.toInt(), minutes.toInt(), seconds.toInt())
}
Run Code Online (Sandbox Code Playgroud)
测试.kt
val oldTime: Long = 1534854646
val currentTime: Long = 1534857527
val result = convertFromDuration(currentTime - oldTime)
Log.i("TAG", result.toString())
Run Code Online (Sandbox Code Playgroud)
输出:
I/TAG: 0h : 48m : 01s
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3382 次 |
| 最近记录: |