invirt.utils.datetime.kt Maven / Gradle / Ivy
package invirt.utils
import java.time.Instant
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
fun Instant.plusDays(days: Int): Instant {
return this.plus(days.toLong(), ChronoUnit.DAYS)
}
fun Instant.minusDays(days: Int): Instant {
return this.minus(days.toLong(), ChronoUnit.DAYS)
}
fun LocalDate.dayOfMonthSuffix(): String {
if (dayOfMonth in 11..13) {
return "th";
}
return when (dayOfMonth % 10) {
1 -> "st";
2 -> "nd";
3 -> "rd";
else -> "th"
}
}
private val REGEX_DAY_PATTERN = "d(\\s|$)".toRegex()
fun LocalDate.patternWithDaySuffix(pattern: String): String {
return pattern.replace(REGEX_DAY_PATTERN, "d'" + dayOfMonthSuffix() + "'$1")
}
fun LocalDate.formatWithDaySuffix(pattern: String): String {
return format(DateTimeFormatter.ofPattern(patternWithDaySuffix(pattern)))
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy