Created
August 22, 2023 15:26
-
-
Save MousaHalaseh/3cd8282471226ebfa1c1e267d4b5e66a to your computer and use it in GitHub Desktop.
Find the day of week for any date given after 1751 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
package sample | |
import java.time.DayOfWeek.* | |
import java.time.LocalDate | |
class DayOfWeekCalculator | |
fun main() { | |
val date = LocalDate.now() | |
println("The day of week is ${calculateDayOfWeek(date)} for $date") | |
} | |
fun calculateDayOfWeek(date: LocalDate): String { | |
// 1. Take the last two digits of the year. | |
val step1: Int = date.year.toString().substring(2).toInt() | |
// 2. Divide it by 4 and discard any remainder. | |
val step2: Int = step1 / 4 | |
//3. Add the day of the month to the value obtained in the previous step. | |
val step3: Int = step2 + date.dayOfMonth | |
// 4. Add the month code to the value obtained in the previous step. | |
val step4: Int = step3 + MonthCode.codeFor(date.month.value) | |
// 5. If the date is in Jan or Feb of a leap year; subtract 1 from the previous step. | |
val step5: Int = if (date.isLeapYear && listOf(1, 2).contains(date.month.value)) step4 - 1 else step4 | |
// 6. Add the century code to the value from the previous step. | |
val step6: Int = step5 + CenturyCode.codeFor(date.year.toString().substring(0, 2).toInt()) | |
// 7. Add the last two digits of the year to the value from the previous step. | |
val step7: Int = step6 + date.year.toString().substring(2).toInt() | |
// 8. Divide the value by 7 and take the remainder. | |
val step8: Int = step7 % 7 | |
// 9. Find the day of the week that corresponds to the remainder obtained in the previous step. | |
return DayOfWeek.dayFor(step8) | |
} | |
enum class MonthCode( | |
val month: Int, | |
val code: Int, | |
) { | |
JAN(1, 1), | |
FEB(2, 4), | |
MAR(3, 4), | |
APR(4, 0), | |
MAY(5, 2), | |
JUN(6, 5), | |
JULY(7, 0), | |
AUG(8, 3), | |
SEPT(9, 6), | |
OCT(10, 1), | |
NOV(11, 4), | |
DEC(12, 6); | |
companion object { | |
fun codeFor(mo: Int): Int { | |
values().find { it.month == mo }.also { return it?.code ?: -1 } | |
} | |
} | |
} | |
enum class CenturyCode( | |
val century: Int, | |
val code: Int, | |
) { | |
EIGHTEENTH(17, 4), | |
NINETEENTH(18, 2), | |
TWENTIETH(19, 0), | |
TWNETYFIRST(20, 6); | |
companion object { | |
fun codeFor(cent: Int): Int { | |
CenturyCode.values().find { it.century == cent }.also { return it?.code ?: -1 } | |
} | |
} | |
} | |
enum class DayOfWeek( | |
val day: String, | |
val code: Int, | |
) { | |
SAT(SATURDAY.name, 0), | |
SUN(SUNDAY.name, 1), | |
MON(MONDAY.name, 2), | |
TUE(TUESDAY.name, 3), | |
WED(WEDNESDAY.name, 4), | |
THUR(THURSDAY.name, 5), | |
FRI(FRIDAY.name, 6); | |
companion object { | |
fun dayFor(cd: Int): String { | |
DayOfWeek.values().find { it.code == cd }.also { return it?.day ?: "Funday" } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment