日期相关util

This commit is contained in:
renhaoting 2025-11-20 10:33:44 +08:00
parent 086bb93212
commit 2aea918304
1 changed files with 159 additions and 0 deletions

View File

@ -0,0 +1,159 @@
package com.ama.core.architecture.util
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.TimeUnit
object DateUtil {
private const val FORMAT_DATE = "yyyy-MM-dd"
private const val FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss"
fun getCurrentDateTime(format: String = FORMAT_DATETIME): String {
return SimpleDateFormat(format, Locale.getDefault()).format(Date())
}
fun timestampToDate(timestamp: Long, format: String = FORMAT_DATE): String {
return SimpleDateFormat(format, Locale.getDefault()).format(Date(timestamp))
}
fun stringToDate(dateString: String, format: String = FORMAT_DATE): Date? {
return try {
SimpleDateFormat(format, Locale.getDefault()).parse(dateString)
} catch (e: Exception) {
e.printStackTrace()
null
}
}
fun convertDateFormat(dateString: String, fromFormat: String, toFormat: String): String {
return try {
val date = SimpleDateFormat(fromFormat, Locale.getDefault()).parse(dateString)
SimpleDateFormat(toFormat, Locale.getDefault()).format(date)
} catch (e: Exception) {
e.printStackTrace()
dateString
}
}
/**
* 给定时间是否超过指定周期
*/
fun isPeriodExpired(targetTime: Any, period: Long, timeUnit: TimeUnit): Boolean {
val targetTimestamp = when (targetTime) {
is Long -> targetTime
is Date -> targetTime.time
is String -> stringToTimestamp(targetTime) ?: 0L
else -> 0L
}
if (targetTimestamp == 0L) return false
val currentTime = System.currentTimeMillis()
val periodMillis = timeUnit.toMillis(period)
return currentTime - targetTimestamp > periodMillis
}
/**
* 从起始时间到现在已经过去多少天
*/
fun getDaysPassed(startTime: Any): Long {
val startTimestamp = when (startTime) {
is Long -> startTime
is Date -> startTime.time
is String -> stringToTimestamp(startTime) ?: 0L
else -> 0L
}
if (startTimestamp == 0L) return 0L
val currentTime = System.currentTimeMillis()
val diffMillis = currentTime - startTimestamp
return TimeUnit.MILLISECONDS.toDays(diffMillis)
}
/**
* 两个时间点之间的天数差
*/
fun getDaysBetween(startTime: Any, endTime: Any): Long {
val startTimestamp = convertToTimestamp(startTime)
val endTimestamp = convertToTimestamp(endTime)
if (startTimestamp == 0L || endTimestamp == 0L) return 0L
val diffMillis = endTimestamp - startTimestamp
return TimeUnit.MILLISECONDS.toDays(diffMillis)
}
/**
* 判断今天是否是特定日期
*/
fun isToday(targetTime: Any): Boolean {
val targetDate = when (targetTime) {
is Long -> Date(targetTime)
is Date -> targetTime
is String -> stringToDate(targetTime)
else -> null
}
return targetDate?.let { isSameDay(it, Date()) } ?: false
}
/**
* 两个日期是否是同一天
*/
fun isSameDay(date1: Date, date2: Date): Boolean {
val cal1 = Calendar.getInstance().apply { time = date1 }
val cal2 = Calendar.getInstance().apply { time = date2 }
return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) &&
cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH)
}
/**
* 在指定日期上添加天数
*/
fun addDays(date: Date, days: Int): Date {
val cal = Calendar.getInstance().apply { time = date }
cal.add(Calendar.DAY_OF_MONTH, days)
return cal.time
}
fun getFirstDayOfMonth(date: Date = Date()): Date {
val cal = Calendar.getInstance().apply { time = date }
cal.set(Calendar.DAY_OF_MONTH, 1)
return cal.time
}
fun getLastDayOfMonth(date: Date = Date()): Date {
val cal = Calendar.getInstance().apply { time = date }
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH))
return cal.time
}
// 私有工具方法
private fun stringToTimestamp(dateString: String, format: String = FORMAT_DATE): Long? {
return stringToDate(dateString, format)?.time
}
private fun convertToTimestamp(time: Any): Long {
return when (time) {
is Long -> time
is Date -> time.time
is String -> stringToTimestamp(time) ?: 0L
else -> 0L
}
}
}