Files
sodalive-backend-spring-boot/src/main/kotlin/kr/co/vividnext/sodalive/extensions/LocalDateTimeExtensions.kt

29 lines
960 B
Kotlin

package kr.co.vividnext.sodalive.extensions
import java.time.Duration
import java.time.LocalDateTime
import java.time.ZoneId
private val DEFAULT_KST_ZONE_ID: ZoneId = ZoneId.of("Asia/Seoul")
private val UTC_ZONE_ID: ZoneId = ZoneId.of("UTC")
fun LocalDateTime.getTimeAgoString(): String {
val now = LocalDateTime.now()
val duration = Duration.between(this, now)
return when {
duration.toMinutes() < 1 -> "방금 전"
duration.toMinutes() < 60 -> "${duration.toMinutes()}분전"
duration.toHours() < 24 -> "${duration.toHours()}시간전"
duration.toDays() < 30 -> "${duration.toDays()}일전"
duration.toDays() < 365 -> "${duration.toDays() / 30}개월전"
else -> "${duration.toDays() / 365}년전"
}
}
fun LocalDateTime.convertToUtc(timeZone: ZoneId = DEFAULT_KST_ZONE_ID): LocalDateTime {
return atZone(timeZone)
.withZoneSameInstant(UTC_ZONE_ID)
.toLocalDateTime()
}