例如,我從服務器獲取 UTC 日期
"endValidityDate": "2021-11-18T22:59:59Z"
我想知道從現在開始計算剩余天數的最佳方法是什么。
這是我現在得到的:
我是從創建2天的日期現在為:
DateTime.now().plusSeconds(172800)
我正在將它決議為一個DateTimejoda,如果你這么說,我可以使用其他的。
在做不同的日子時,我這樣做
val diff = endValidityDate.toDate().time - Date().time
val daysRemaining = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS)
return if (daysRemaining > 1) "$daysRemaining days}"
else TimeUnit.DAYS.convert(diff, TimeUnit.SECONDS).toString()
我試圖實作的場景是:
如果剩余天數超過 1(24 小時),則例如列印“剩余 2 天”,而不是顯示“剩余 1 天”,則只需添加一個計時器:
“0h 43m 3s”。
為了做計時器,我只是減去現在剩余的時間
val expireDate = LocalDateTime.now()
.plusSeconds(uiState.endValidityDate.timeLeft.toLong())
.toEpochSecond(ZoneOffset.UTC)
val currentTime = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC)
然后每一秒它都會發生我這樣列印:
val duration = Duration.ofSeconds(it)
binding.myTextView.text = String.format(
"dh: dm: ds",
duration.seconds / 3600,
(duration.seconds % 3600) / 60,
duration.seconds % 60,
)
但我沒有得到 2 天,我只是作為輸出得到:
00h: 33m: 50s
所以,我在這里遇到了一些問題,例如:
這是最佳解決方案嗎?如果沒有,你能描述一個更好的我可以實作我的目標嗎?為什么我的計時器顯示為00h: 13m: 813s?我是不是做錯了正則運算式,還是因為 epochSeconds?
達到
在嘗試將其列印到設備時,給定來自服務器的 UTC 日期,則它應遵循此規則。
1.- 如果剩余天數 > 1 天,則列印“剩余 N 天”
2.- 如果剩余天數 <= 1,則列印一個計時器(它已經完成,問題是如何正確列印)。
- 最少 1 位(0h 2m 1s)
- 最多 2 位(1h 23m 3s)
筆記 :
我正在使用Java 8 我也可以改變我做倒計時的方式,如果那是問題的話,我可以使用毫秒而不是 epochSeconds。
uj5u.com熱心網友回復:
您可以使用 a ZonedDateTimefor now和 future datetime,然后計算 aDuration.between而不是先計算剩余秒數,然后使用 a Duration.ofSeconds()。
這是一個 Kotlin 示例:
fun main() {
val utc = ZoneId.of("UTC")
val now = ZonedDateTime.now(utc)
val twoDaysFromNow = now.plusDays(2)
val remaining = Duration.between(now, twoDaysFromNow)
println(
String.format("dh: dm: ds",
remaining.seconds / 3600,
(remaining.seconds % 3600) / 60,
remaining.seconds % 60
)
)
}
輸出: 48h: 00m: 00s
如果您只對剩余的全天感興趣,請考慮使用ChronoUnit.DAYS.between,可能是這樣的:
fun main() {
val utc = ZoneId.of("UTC")
val now = ZonedDateTime.now(utc)
val twoDaysFromNow = now.plusDays(2)
val remainingDays = ChronoUnit.DAYS.between(now, twoDaysFromNow)
println(
String.format("%d days", remainingDays)
)
}
輸出: 2 days
額外的:
由于我不清楚您嘗試使用哪種資料型別來計算有效期結束前的剩余時間,因此您必須在問題中提供更詳細的資訊或使用以下方法之一進行選擇fun:
通過一個 ZonedDateTime
private fun getRemainingTime(endValidityDate: ZonedDateTime): String {
// get the current moment in time as a ZonedDateTime in UTC
val now = ZonedDateTime.now(ZoneId.of("UTC"))
// calculate the difference directly
val timeLeft = Duration.between(now, endValidityDate)
// return the messages depending on hours left
return if (timeLeft.toHours() >= 24) "${timeLeft.toDays()} days"
else String.format("dh: dm: ds",
timeLeft.toHours(),
timeLeft.toMinutes() % 60,
timeLeft.toSeconds() % 60)
}
通過一個 Instant
private fun getRemainingTime(endValidityDate: Instant): String {
// get the current moment in time, this time as an Instant directly
val now = Instant.now()
// calculate the difference
val timeLeft = Duration.between(now, endValidityDate)
// return the messages depending on hours left
return if (timeLeft.toHours() >= 24) "${timeLeft.toDays()} days"
else String.format("dh: dm: ds",
timeLeft.toHours(),
timeLeft.toMinutes() % 60,
timeLeft.toSeconds() % 60)
}
傳球String直接
private fun getRemainingTime(endValidityDate: String): String {
// get the current moment in time as a ZonedDateTime in UTC
val now = ZonedDateTime.now(ZoneId.of("UTC"))
// parse the endValidtyDate String
val then = ZonedDateTime.parse(endValidityDate)
// calculate the difference
val timeLeft = Duration.between(now, then)
// return the messages depending on hours left
return if (timeLeft.toHours() >= 24) "${timeLeft.toDays()} days"
else String.format("dh: dm: ds",
timeLeft.toHours(),
timeLeft.toMinutes() % 60,
timeLeft.toSeconds() % 60)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/363351.html
上一篇:如何處理這種格式的時間戳?
下一篇:“系列的真值不明確。使用a.empty、a.bool()、a.item()、a.any()或a.all()。”將函式應用于資料框時
