I have this code that works :
private fun getCycleBoundaries(cycleNumber: Int, year: Int): Array<Date> {
return when (cycleNumber) {
1 -> arrayOf(DATE_FORMATER.parse("$year-01-01T00:00:00"), DATE_FORMATER.parse("$year-03-31T23:59:59"))
2 -> arrayOf(DATE_FORMATER.parse("$year-04-01T00:00:00"), DATE_FORMATER.parse("$year-06-30T23:59:59"))
3 -> arrayOf(DATE_FORMATER.parse("$year-07-01T00:00:00"), DATE_FORMATER.parse("$year-09-30T23:59:59"))
4 -> arrayOf(DATE_FORMATER.parse("$year-10-01T00:00:00"), DATE_FORMATER.parse("$year-12-31T23:59:59"))
else -> throw IllegalArgumentException("$cycleNumber is nor a valid cycle number")
}
}
This code takes a cycle number : 1 or 2 or 3 or 4 and a year And it returns an arrays of 2 dates (the start and the end of the quarter)
For example if i call :
getCycleBoundaries(1, 2019)
I expect :
2019-01-01T00:00:00 and 2019-03-31T23:59:59
As said it works but i'm not happy with the code and was trying to do it better. Is there a better way to achieve this ?
Thanks