添加了界面及逻辑
This commit is contained in:
@@ -5,6 +5,7 @@ import kotlinx.coroutines.flow.flow
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
import kotlin.math.abs
|
||||
|
||||
/**
|
||||
* 时间格式化工具类
|
||||
@@ -47,4 +48,66 @@ object DateTimeUtils {
|
||||
delay(intervalMillis)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析日期时间字符串
|
||||
* @param timeString 格式为 "yyyy-MM-dd HH:mm:ss" 的字符串
|
||||
* @return Date 对象,解析失败返回 null
|
||||
*/
|
||||
fun parseDateTime(timeString: String?): Date? {
|
||||
return try {
|
||||
if (timeString == null) return null
|
||||
val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
|
||||
format.parse(timeString)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断给定时间是否距离当前时间超过36小时
|
||||
* @param timeInMillis 时间戳(毫秒)
|
||||
* @return true 表示超过36小时,false 表示未超过
|
||||
*/
|
||||
fun isMoreThan36HoursFromNow(timeInMillis: Long): Boolean {
|
||||
val currentTime = System.currentTimeMillis()
|
||||
val timeDifference = currentTime - timeInMillis
|
||||
val hoursDifference = timeDifference / (1000 * 60 * 60) // 毫秒转小时
|
||||
return hoursDifference >= 36
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取时间间隔描述
|
||||
*/
|
||||
fun getTimeAgo(date: Date?): String {
|
||||
if (date == null) return "未知时间"
|
||||
|
||||
val now = Date()
|
||||
val diffMillis = now.time - date.time
|
||||
|
||||
// 如果是未来时间
|
||||
if (diffMillis < 0) {
|
||||
val futureHours = abs(diffMillis) / (1000 * 60 * 60)
|
||||
return if (futureHours < 24) {
|
||||
"未来 $futureHours 小时"
|
||||
} else {
|
||||
val days = futureHours / 24
|
||||
"未来 $days 天"
|
||||
}
|
||||
}
|
||||
|
||||
// 过去时间
|
||||
val hours = diffMillis / (1000 * 60 * 60)
|
||||
return when {
|
||||
hours < 1 -> "刚刚"
|
||||
hours < 24 -> "${hours}小时前"
|
||||
// hours < 48 -> "昨天"
|
||||
// hours < 72 -> "前天"
|
||||
else -> {
|
||||
val days = hours / 24
|
||||
"${days}天前"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user