Files
rider-pro-android-app/app/src/main/java/com/aiosman/riderpro/exp/Date.kt
2024-08-24 21:59:16 +08:00

40 lines
1.1 KiB
Kotlin

package com.aiosman.riderpro.exp
import android.icu.text.SimpleDateFormat
import android.icu.util.Calendar
import com.aiosman.riderpro.data.api.ApiClient
import java.util.Date
import java.util.Locale
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"
}
}
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)
}