Created
July 22, 2022 17:29
-
-
Save alexfu/4b995be4708b3676eed483b377a766fa to your computer and use it in GitHub Desktop.
Do you wish there was an easier way to write dates 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.LocalDate | |
import java.time.LocalDateTime | |
import java.time.Month | |
import java.time.ZonedDateTime | |
import java.util.TimeZone | |
fun January(day: Int, year: Int) = LocalDate.of(year, Month.JANUARY, day) | |
fun February(day: Int, year: Int) = LocalDate.of(year, Month.FEBRUARY, day) | |
fun March(day: Int, year: Int) = LocalDate.of(year, Month.MARCH, day) | |
fun April(day: Int, year: Int) = LocalDate.of(year, Month.APRIL, day) | |
fun May(day: Int, year: Int) = LocalDate.of(year, Month.MAY, day) | |
fun June(day: Int, year: Int) = LocalDate.of(year, Month.JUNE, day) | |
fun July(day: Int, year: Int) = LocalDate.of(year, Month.JULY, day) | |
fun August(day: Int, year: Int) = LocalDate.of(year, Month.AUGUST, day) | |
fun September(day: Int, year: Int) = LocalDate.of(year, Month.SEPTEMBER, day) | |
fun October(day: Int, year: Int) = LocalDate.of(year, Month.OCTOBER, day) | |
fun November(day: Int, year: Int) = LocalDate.of(year, Month.NOVEMBER, day) | |
fun December(day: Int, year: Int) = LocalDate.of(year, Month.DECEMBER, day) | |
fun LocalDateTime.inZone(zone: TimeZone): ZonedDateTime { | |
return ZonedDateTime.of(this, zone.toZoneId()) | |
} |
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
object TimeZones { | |
val americaNewYork: TimeZone = TimeZone.getTimeZone("America/New_York") | |
} | |
July(22, 2022).atTime(8, 0) // LocalDateTime example | |
July(22, 2022).atTime(8, 0).inZone(TimeZones.americaNewYork)) // ZonedDateTime example |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment