Skip to content

Instantly share code, notes, and snippets.

@firecatmusic
Created October 10, 2022 07:04
Show Gist options
  • Save firecatmusic/0836fac4d4206b75991409429458b079 to your computer and use it in GitHub Desktop.
Save firecatmusic/0836fac4d4206b75991409429458b079 to your computer and use it in GitHub Desktop.
Date class helper
import org.junit.Test
import java.time.DayOfWeek
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.temporal.TemporalAdjusters
import java.util.*
class DateUtil {
@Test
fun test() {
//day
val now = LocalDateTime.now()
println("Now: " + toString(now) + ", in mills: " + toMills(now))
println("Start of day: " + toString(startOfDay()))
println("End of day: " + toString(endOfDay()))
println("Does '" + toString(now) + "' belong to the current day? > " + belongsToCurrentDay(now))
val yesterday = now.minusDays(1)
println("Does '" + toString(yesterday) + "' belong to the current day? > " + belongsToCurrentDay(yesterday))
val tomorrow = now.plusDays(1)
println("Does '" + toString(tomorrow) + "' belong to the current day? > " + belongsToCurrentDay(tomorrow))
//week
println("Start of week: " + toString(startOfWeek()))
println("End of week: " + toString(endOfWeek()))
println("Does '" + toString(now) + "' belong to the current week? > " + belongsToCurrentWeek(now))
val previousWeek = now.minusWeeks(1)
println("Does '" + toString(previousWeek) + "' belong to the current week? > " + belongsToCurrentWeek(previousWeek))
val nextWeek = now.plusWeeks(1)
println("Does '" + toString(nextWeek) + "' belong to the current week? > " + belongsToCurrentWeek(nextWeek))
//month
println("Start of month: " + toString(startOfMonth()))
println("End of month: " + toString(endOfMonth()))
println("Does '" + toString(now) + "' belong to the current month? > " + belongsToCurrentMonth(now))
val previousMonth = now.minusMonths(1)
println("Does '" + toString(previousMonth) + "' belong to the current month? > " + belongsToCurrentMonth(previousMonth))
val nextMonth = now.plusMonths(1)
println("Does '" + toString(nextMonth) + "' belong to the current month? > " + belongsToCurrentMonth(nextMonth))
}
companion object {
private val DEFAULT_ZONE_ID = ZoneId.of("UTC")
fun startOfDay(): LocalDateTime {
return LocalDateTime.now(DEFAULT_ZONE_ID).with(LocalTime.MIN)
}
fun endOfDay(): LocalDateTime {
return LocalDateTime.now(DEFAULT_ZONE_ID).with(LocalTime.MAX)
}
fun belongsToCurrentDay(localDateTime: LocalDateTime): Boolean {
return localDateTime.isAfter(startOfDay()) && localDateTime.isBefore(endOfDay())
}
//note that week starts with Monday
fun startOfWeek(): LocalDateTime {
return LocalDateTime.now(DEFAULT_ZONE_ID)
.with(LocalTime.MIN)
.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY))
}
//note that week ends with Sunday
fun endOfWeek(): LocalDateTime {
return LocalDateTime.now(DEFAULT_ZONE_ID)
.with(LocalTime.MAX)
.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY))
}
fun belongsToCurrentWeek(localDateTime: LocalDateTime): Boolean {
return localDateTime.isAfter(startOfWeek()) && localDateTime.isBefore(endOfWeek())
}
fun startOfMonth(): LocalDateTime {
return LocalDateTime.now(DEFAULT_ZONE_ID)
.with(LocalTime.MIN)
.with(TemporalAdjusters.firstDayOfMonth())
}
fun endOfMonth(): LocalDateTime {
return LocalDateTime.now(DEFAULT_ZONE_ID)
.with(LocalTime.MAX)
.with(TemporalAdjusters.lastDayOfMonth())
}
fun belongsToCurrentMonth(localDateTime: LocalDateTime): Boolean {
return localDateTime.isAfter(startOfMonth()) && localDateTime.isBefore(endOfMonth())
}
fun startOfYear(): LocalDateTime {
return LocalDateTime.now(DEFAULT_ZONE_ID)
.with(LocalTime.MIN)
.with(TemporalAdjusters.firstDayOfYear())
}
fun endOfYear(): LocalDateTime {
return LocalDateTime.now(DEFAULT_ZONE_ID)
.with(LocalTime.MAX)
.with(TemporalAdjusters.lastDayOfYear())
}
fun belongsToCurrentYear(localDateTime: LocalDateTime): Boolean {
return localDateTime.isAfter(startOfYear()) && localDateTime.isBefore(endOfYear())
}
fun toMills(localDateTime: LocalDateTime): Long {
return localDateTime.atZone(DEFAULT_ZONE_ID).toInstant().toEpochMilli()
}
fun toDate(localDateTime: LocalDateTime): Date {
return Date.from(localDateTime.atZone(DEFAULT_ZONE_ID).toInstant())
}
fun toString(localDateTime: LocalDateTime): String {
return localDateTime.format(DateTimeFormatter.ISO_DATE_TIME)
}
}
}
@firecatmusic
Copy link
Author

founded on stackoverflow, i forget the source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment