Last active
July 17, 2019 06:21
-
-
Save mohanmanu484/a3bc8e1752b8accb8dddaedc0bf2d967 to your computer and use it in GitHub Desktop.
Calendar Item
This file contains hidden or 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 calendar | |
| import java.text.SimpleDateFormat | |
| import java.util.* | |
| import java.text.DateFormat | |
| class KCalendar { | |
| private val holidays = listOf(SpecificDayHoliday(23, 6), | |
| HoliDayThatCantHappenOnWeekend(25, 6), | |
| HoliDayThatOccursCertainDayInAMonth(6, Calendar.MONDAY, 2) | |
| ) | |
| fun getWeekDayCount(date1: Date, date2: Date): Int { | |
| val cal1 = Calendar.getInstance().apply { | |
| time = date1 | |
| add(Calendar.DAY_OF_MONTH, 1) | |
| } | |
| val cal2 = Calendar.getInstance().apply { | |
| time = date2 | |
| add(Calendar.DAY_OF_MONTH, -1) | |
| } | |
| var tempCal = cal1 | |
| var count = 0 | |
| while (tempCal < cal2) { | |
| if (tempCal.isWeekDay() && !tempCal.isHoliday()) { | |
| count++ | |
| } | |
| tempCal = tempCal.apply { | |
| add(Calendar.DATE, 1) | |
| } | |
| } | |
| return count | |
| } | |
| private fun Calendar.isHoliday(): Boolean { | |
| holidays.forEach { | |
| val holiday = it.getHolidayForCalendar(get(Calendar.YEAR)) | |
| if (isSameDay(holiday, this)) { | |
| return true | |
| } | |
| } | |
| return false | |
| } | |
| private fun isSameDay(holiday: Calendar, calendar: Calendar): Boolean { | |
| return (holiday.get(Calendar.YEAR) == calendar.get(Calendar.YEAR) && | |
| (holiday.get(Calendar.MONTH) == calendar.get(Calendar.MONTH) && | |
| (holiday.get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH)))) | |
| } | |
| } | |
| inline fun Calendar.isWeekDay(): Boolean { | |
| return get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY | |
| } | |
| fun main(args: Array<String>) { | |
| val kCalendar = KCalendar() | |
| val cal1 = Calendar.getInstance().apply { | |
| set(Calendar.YEAR, 2019) | |
| set(Calendar.MONTH, 6) | |
| set(Calendar.DAY_OF_MONTH, 1) | |
| } | |
| val cal2 = Calendar.getInstance().apply { | |
| set(Calendar.YEAR, 2019) | |
| set(Calendar.MONTH, 6) | |
| set(Calendar.DAY_OF_MONTH, 31) | |
| } | |
| println(kCalendar.getWeekDayCount(cal1.time, cal2.time)) | |
| } | |
| interface Holiday { | |
| fun getHolidayForCalendar(year: Int): Calendar | |
| } | |
| class SpecificDayHoliday(val day: Int, val month: Int) : Holiday { | |
| override fun getHolidayForCalendar(year: Int): Calendar { | |
| return Calendar.getInstance().apply { | |
| set(Calendar.YEAR, year) | |
| set(Calendar.MONTH, month) | |
| set(Calendar.DAY_OF_MONTH, day) | |
| } | |
| } | |
| } | |
| class HoliDayThatOccursCertainDayInAMonth(val month: Int, val dayOfWeek: Int, val dayOfWeekInMonth: Int) : Holiday { | |
| override fun getHolidayForCalendar(year: Int): Calendar { | |
| return Calendar.getInstance().apply { | |
| set(Calendar.YEAR, year) | |
| set(Calendar.MONTH, month) | |
| set(Calendar.DAY_OF_WEEK, dayOfWeek) | |
| set(Calendar.DAY_OF_WEEK_IN_MONTH, dayOfWeekInMonth) | |
| } | |
| } | |
| } | |
| class HoliDayThatCantHappenOnWeekend(val day: Int, val month: Int) : Holiday { | |
| override fun getHolidayForCalendar(year: Int): Calendar { | |
| val cal = Calendar.getInstance().apply { | |
| set(Calendar.YEAR, year) | |
| set(Calendar.MONTH, month) | |
| set(Calendar.DAY_OF_MONTH, day) | |
| } | |
| return if (cal.isWeekDay()) { | |
| cal | |
| } else { | |
| getNextWeekDay(cal) | |
| } | |
| } | |
| private fun getNextWeekDay(cal: Calendar): Calendar { | |
| while (!cal.isWeekDay()) { | |
| cal.add(Calendar.DAY_OF_MONTH, 1) | |
| } | |
| return cal | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment