Files
rider-pro-android-app/app/src/main/java/com/aiosman/riderpro/exp/Date.kt

46 lines
1.2 KiB
Kotlin
Raw Normal View History

2024-08-20 19:48:12 +08:00
package com.aiosman.riderpro.exp
import android.icu.text.SimpleDateFormat
2024-08-24 21:59:16 +08:00
import android.icu.util.Calendar
2024-08-20 19:48:12 +08:00
import com.aiosman.riderpro.data.api.ApiClient
import java.util.Date
import java.util.Locale
2024-08-24 23:11:20 +08:00
/**
* 格式化时间为 xx
*/
2024-08-20 19:48:12 +08:00
fun Date.timeAgo(): String {
val now = Date()
val diffInMillis = now.time - this.time
val seconds = diffInMillis / 1000
val minutes = seconds / 60
val hours = minutes / 60
val days = hours / 24
val years = days / 365
return when {
seconds < 60 -> "$seconds seconds ago"
minutes < 60 -> "$minutes minutes ago"
hours < 24 -> "$hours hours ago"
days < 365 -> "$days days ago"
else -> "$years years ago"
}
2024-08-24 21:59:16 +08:00
}
2024-08-24 23:11:20 +08:00
/**
* 格式化时间为 xx-xx
*/
2024-08-24 21:59:16 +08:00
fun Date.formatPostTime(): String {
val now = Calendar.getInstance()
val calendar = Calendar.getInstance()
calendar.time = this
val year = calendar.get(Calendar.YEAR)
var nowYear = now.get(Calendar.YEAR)
val dateFormat = if (year == nowYear) {
SimpleDateFormat("MM-dd", Locale.getDefault())
} else {
SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
}
return dateFormat.format(this)
2024-08-20 19:48:12 +08:00
}