添加了顶部时间更新
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package com.sw.inbound.utils
|
||||
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
|
||||
/**
|
||||
* 时间格式化工具类
|
||||
*/
|
||||
object DateTimeUtils {
|
||||
|
||||
/**
|
||||
* 获取完整中文日期格式(示例:2025年6月11日 星期三)
|
||||
*/
|
||||
fun getChineseDateString(date: Date = Date()): String {
|
||||
return SimpleDateFormat("yyyy年M月d日 EEEE", Locale.CHINA).format(date)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取带时间的完整中文格式(示例:2025年6月11日 星期三 14:30)
|
||||
*/
|
||||
fun getChineseDateTimeString(date: Date = Date()): String {
|
||||
return SimpleDateFormat("yyyy年M月d日 EEEE HH:mm:ss", Locale.CHINA).format(date)
|
||||
}
|
||||
|
||||
// 使用线程安全的日期格式化(避免 SimpleDateFormat 的线程安全问题)
|
||||
private val dateFormat by lazy {
|
||||
SimpleDateFormat("yyyy年M月d日 EEEE", Locale.CHINA)
|
||||
}
|
||||
private val timeFormat by lazy {
|
||||
SimpleDateFormat("HH:mm:ss", Locale.CHINA)
|
||||
}
|
||||
|
||||
fun getChineseDateTimePair(date: Date = Date()): Pair<String, String> {
|
||||
return dateFormat.format(date) to timeFormat.format(date)
|
||||
}
|
||||
|
||||
/**
|
||||
* 实时时间流(每秒更新)
|
||||
* @param intervalMillis 更新间隔(默认1秒)
|
||||
*/
|
||||
fun realTimeChineseDateFlow(intervalMillis: Long = 1000) = flow {
|
||||
while (true) {
|
||||
emit(getChineseDateTimePair())
|
||||
delay(intervalMillis)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user