Funny thing is that this Calendar is returning first day of the week as next Sunday which is completely illogical.
So I made index conversion function to sort days. It worked for January, but as soon as I moved to February, out of nowhere, first week of the February is last week of January for some reason.
So days are now fine, but weeks are out of order for some reason. Even Calendar.DAY_OF_WEEK
is returning wrong data for specific Calendar.WEEK_OF_MONTH
. There is zero logic in this native Calendar
and I cant make simple iterator over all days of the year to form some usable structure for my custom Calendar.
Code:
private fun loadDatePickerYear(year: Int): DatePickerYear{
val cal = Calendar.getInstance(Locale.forLanguageTag("cs"))
cal.set(Calendar.YEAR, year)
val months = mutableListOf<DatePickerMonth>()
(0..11).forEach { month->
months.add(loadDatePickerMonth(cal, month))
}
return DatePickerYear(months, year)
}
private fun loadDatePickerMonth(cal: Calendar, month: Int): DatePickerMonth{
App.log("DatePickerNew: loadDatePickerMonth: $month")
cal.set(Calendar.MONTH, month)
val firstWeek = cal.getActualMinimum(Calendar.WEEK_OF_MONTH)
val lastWeek = cal.getActualMaximum(Calendar.WEEK_OF_MONTH)
val weeks = mutableListOf<DatePickerWeek>()
(firstWeek..lastWeek).forEach { week->
weeks.add(loadDatePickerWeek(cal, week))
}
return DatePickerMonth(weeks, month)
}
private fun loadDatePickerWeek(cal: Calendar, week: Int): DatePickerWeek{
App.log("DatePickerNew: loadDatePickerWeek: $week")
cal.set(Calendar.WEEK_OF_MONTH, week)
val firstDay = cal.getActualMinimum(Calendar.DAY_OF_WEEK)
val lastDay = cal.getActualMaximum(Calendar.DAY_OF_WEEK)
val days = mutableListOf<DatePickerDay>()
(firstDay..lastDay).forEach { day->
days.add(loadDatePickerDay(cal, day))
}
days.sortBy { it.dayOfWeek }
days.forEach {
App.log("DatePickerNew: loadDatePickerWeek: sorted: ${it.dayOfWeek}, ${it.dayAsDate}")
}
return DatePickerWeek(days, week)
}
private fun loadDatePickerDay(cal: Calendar, day: Int): DatePickerDay{
cal.set(Calendar.DAY_OF_WEEK, day)
val date = Date(cal.timeInMillis)
//return day
App.log("DatePickerNew: loadDatePickerDay: $day $date")
return DatePickerDay(date, getEuropeanDayIndex(day)!!)
}
//2, 3, 4, 5, 6, 7, 1
fun getEuropeanDayIndex(dayOfWeek: Int): Int?{
return when(dayOfWeek){
1 -> 7
2 -> 1
3 -> 2
4 -> 3
5 -> 4
6 -> 5
7 -> 6
else -> null
}
}
Logs for January (first 2 weeks):
DatePickerNew: loadDatePickerMonth: 0
DatePickerNew: loadDatePickerWeek: 0
DatePickerNew: loadDatePickerDay: 1 Sun Jan 02 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerDay: 2 Mon Dec 27 11:24:01 GMT 01:00 2021
DatePickerNew: loadDatePickerDay: 3 Tue Dec 28 11:24:01 GMT 01:00 2021
DatePickerNew: loadDatePickerDay: 4 Wed Dec 29 11:24:01 GMT 01:00 2021
DatePickerNew: loadDatePickerDay: 5 Thu Dec 30 11:24:01 GMT 01:00 2021
DatePickerNew: loadDatePickerDay: 6 Fri Dec 31 11:24:01 GMT 01:00 2021
DatePickerNew: loadDatePickerDay: 7 Sat Jan 01 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 1, Mon Dec 27 11:24:01 GMT 01:00 2021
DatePickerNew: loadDatePickerWeek: sorted: 2, Tue Dec 28 11:24:01 GMT 01:00 2021
DatePickerNew: loadDatePickerWeek: sorted: 3, Wed Dec 29 11:24:01 GMT 01:00 2021
DatePickerNew: loadDatePickerWeek: sorted: 4, Thu Dec 30 11:24:01 GMT 01:00 2021
DatePickerNew: loadDatePickerWeek: sorted: 5, Fri Dec 31 11:24:01 GMT 01:00 2021
DatePickerNew: loadDatePickerWeek: sorted: 6, Sat Jan 01 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 7, Sun Jan 02 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: 1
DatePickerNew: loadDatePickerDay: 1 Sun Jan 09 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerDay: 2 Mon Jan 03 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerDay: 3 Tue Jan 04 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerDay: 4 Wed Jan 05 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerDay: 5 Thu Jan 06 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerDay: 6 Fri Jan 07 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerDay: 7 Sat Jan 08 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 1, Mon Jan 03 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 2, Tue Jan 04 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 3, Wed Jan 05 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 4, Thu Jan 06 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 5, Fri Jan 07 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 6, Sat Jan 08 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 7, Sun Jan 09 11:24:01 GMT 01:00 2022
Logs for February (first 2 weeks):
DatePickerNew: loadDatePickerMonth: 1
DatePickerNew: loadDatePickerWeek: 0
DatePickerNew: loadDatePickerDay: 1 Sun Jan 30 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerDay: 2 Mon Jan 24 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerDay: 3 Tue Jan 25 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerDay: 4 Wed Jan 26 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerDay: 5 Thu Jan 27 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerDay: 6 Fri Jan 28 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerDay: 7 Sat Jan 29 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 1, Mon Jan 24 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 2, Tue Jan 25 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 3, Wed Jan 26 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 4, Thu Jan 27 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 5, Fri Jan 28 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 6, Sat Jan 29 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 7, Sun Jan 30 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: 1
DatePickerNew: loadDatePickerDay: 1 Sun Jan 09 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerDay: 2 Mon Jan 03 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerDay: 3 Tue Jan 04 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerDay: 4 Wed Jan 05 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerDay: 5 Thu Jan 06 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerDay: 6 Fri Jan 07 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerDay: 7 Sat Jan 08 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 1, Mon Jan 03 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 2, Tue Jan 04 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 3, Wed Jan 05 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 4, Thu Jan 06 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 5, Fri Jan 07 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 6, Sat Jan 08 11:24:01 GMT 01:00 2022
DatePickerNew: loadDatePickerWeek: sorted: 7, Sun Jan 09 11:24:01 GMT 01:00 2022
CodePudding user response:
if you are making custom calendar then make the layout start from "Monday" and
get week start day as monday like:
Actually you have not shared your customcalender code so this is what i
get from current question
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, Calendar.MONDAY-
calendar.get(Calendar.DAY_OF_WEEK));
Log.w(TAG, "first day of week" calendar.getTime());
CodePudding user response:
To fix the week numbers you can maybe do
cal.setFirstDayOfWeek(Calendar.MONDAY);
Right after you create it