Last active
November 16, 2018 10:56
-
-
Save yukithm/f5ffd134efd68687da9ab98da5f0d9c4 to your computer and use it in GitHub Desktop.
TemporalQuery example in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.* | |
import java.time.temporal.ChronoField | |
import java.time.temporal.TemporalAdjusters | |
import java.time.temporal.TemporalQuery | |
import java.time.temporal.UnsupportedTemporalTypeException | |
val year = Year.of(2018) | |
// 感謝祭 | |
// 11月の第4木曜日 | |
val thanksgivingDayQuery = TemporalQuery<LocalDate> { | |
val year = it.get(ChronoField.YEAR) | |
LocalDate.of(year, Month.NOVEMBER, 1) | |
.with(TemporalAdjusters.firstInMonth(DayOfWeek.THURSDAY)) | |
.plusWeeks(3) | |
} | |
// ブラックフライデー | |
// 感謝祭の翌日 | |
val blackFridayQuery = TemporalQuery<LocalDate> { | |
it.query(thanksgivingDayQuery).plusDays(1) | |
} | |
// サイバーマンデー | |
// 感謝祭の翌月曜日 | |
// ただしAmazonのサイバーマンデーセールは12月2週目っぽい | |
val cyberMondayQuery = TemporalQuery<LocalDate> { | |
it.query(thanksgivingDayQuery).with(TemporalAdjusters.next(DayOfWeek.MONDAY)) | |
} | |
val blackFriday = year.query(blackFridayQuery) | |
println(blackFriday) | |
val cyberMonday = year.query(cyberMondayQuery) | |
println(cyberMonday) | |
//-------------------------- | |
// Bonus | |
//-------------------------- | |
// プレミアムフライデー | |
// 月の最後の金曜日 | |
fun YearMonth.premiumFriday() = | |
this.atDay(1).with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY)) | |
val ym = YearMonth.now() | |
println(ym.premiumFriday()) | |
// プレミアムフライデーかどうか | |
fun LocalDate.isPremiumFriday() = | |
YearMonth.from(this).premiumFriday() == this | |
println(LocalDate.now().isPremiumFriday()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment