42 lines
1.3 KiB
Kotlin
42 lines
1.3 KiB
Kotlin
package top.tangyh.lamp.filing.utils
|
|
|
|
import java.text.SimpleDateFormat
|
|
import java.util.*
|
|
|
|
class DateUtil {
|
|
|
|
companion object {
|
|
// 获取当前时间戳
|
|
fun getCurrentTimestamp(): Long {
|
|
return System.currentTimeMillis()
|
|
}
|
|
|
|
// 格式化日期
|
|
fun formatDate(date: Date, format: String = "yyyy-MM-dd HH:mm:ss"): String {
|
|
val sdf = SimpleDateFormat(format)
|
|
return sdf.format(date)
|
|
}
|
|
|
|
// 解析日期字符串
|
|
fun parseDate(dateString: String, format: String = "yyyy-MM-dd HH:mm:ss"): Date? {
|
|
val sdf = SimpleDateFormat(format)
|
|
return try {
|
|
sdf.parse(dateString)
|
|
} catch (e: Exception) {
|
|
null
|
|
}
|
|
}
|
|
|
|
// 计算两个日期之间的天数差
|
|
fun getDaysBetweenDates(date1: Date, date2: Date): Long {
|
|
val diff = Math.abs(date1.time - date2.time)
|
|
return diff / (24 * 60 * 60 * 1000)
|
|
}
|
|
|
|
// 获取当前时间并格式化为 yyyy-MM-dd_HH-mm-ss
|
|
fun getCurrentFormattedTime(format: String = "yyyy-MM-dd_HH-mm-ss"): String {
|
|
val sdf = SimpleDateFormat(format)
|
|
return sdf.format(Date())
|
|
}
|
|
}
|
|
} |