我们在Android 7(API 24/25)中使用Calendar
有一些奇怪的行为。Android 7.x(API24)WEEK_OF_MONTH日历错误?
鉴于这种非常简单的代码:
SimpleDateFormat month_date = new SimpleDateFormat("dd.MM.YYYY");
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Berlin"), Locale.GERMANY);
cal.setFirstDayOfWeek(Calendar.MONDAY);
for (int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month++) {
Calendar start = ((Calendar) cal.clone());
start.set(2017, month, 1);
Calendar end = ((Calendar) start.clone());
end.set(Calendar.DAY_OF_MONTH, end.getActualMaximum(Calendar.DAY_OF_MONTH));
Log.d("CAL", "\n Date Start: " + month_date.format(start.getTime()) + " " +
" WEEK_OF_MONTH: " + start.get(Calendar.WEEK_OF_MONTH)
);
Log.d("CAL", "\n Date End: " + month_date.format(end.getTime()) +
" WEEK_OF_MONTH: " + end.get(Calendar.WEEK_OF_MONTH)
);
}
运行在Android 4.0倍,5.x和6.x显示正确的价值为WEEK_OF_MONTH
:
Date Start: 01.01.2016 WEEK_OF_MONTH: 1
Date End: 31.01.2017 WEEK_OF_MONTH: 6
Date Start: 01.02.2017 WEEK_OF_MONTH: 1
Date End: 28.02.2017 WEEK_OF_MONTH: 5
Date Start: 01.03.2017 WEEK_OF_MONTH: 1
Date End: 31.03.2017 WEEK_OF_MONTH: 5
Date Start: 01.04.2017 WEEK_OF_MONTH: 1
Date End: 30.04.2017 WEEK_OF_MONTH: 5
Date Start: 01.05.2017 WEEK_OF_MONTH: 1
Date End: 31.05.2017 WEEK_OF_MONTH: 5
Date Start: 01.06.2017 WEEK_OF_MONTH: 1
Date End: 30.06.2017 WEEK_OF_MONTH: 5
Date Start: 01.07.2017 WEEK_OF_MONTH: 1
Date End: 31.07.2017 WEEK_OF_MONTH: 6
Date Start: 01.08.2017 WEEK_OF_MONTH: 1
Date End: 31.08.2017 WEEK_OF_MONTH: 5
Date Start: 01.09.2017 WEEK_OF_MONTH: 1
Date End: 30.09.2017 WEEK_OF_MONTH: 5
Date Start: 01.10.2017 WEEK_OF_MONTH: 1
Date End: 31.10.2017 WEEK_OF_MONTH: 6
Date Start: 01.11.2017 WEEK_OF_MONTH: 1
Date End: 30.11.2017 WEEK_OF_MONTH: 5
Date Start: 01.12.2017 WEEK_OF_MONTH: 1
Date End: 31.12.2017 WEEK_OF_MONTH: 5
运行在Android 7.x版破损WEEK_OF_MONTH
:
Date Start: 01.01.2016 WEEK_OF_MONTH: 0
Date End: 31.01.2017 WEEK_OF_MONTH: 5
Date Start: 01.02.2017 WEEK_OF_MONTH: 1
Date End: 28.02.2017 WEEK_OF_MONTH: 5
Date Start: 01.03.2017 WEEK_OF_MONTH: 1
Date End: 31.03.2017 WEEK_OF_MONTH: 5
Date Start: 01.04.2017 WEEK_OF_MONTH: 0
Date End: 30.04.2017 WEEK_OF_MONTH: 4
Date Start: 01.05.2017 WEEK_OF_MONTH: 1
Date End: 31.05.2017 WEEK_OF_MONTH: 5
Date Start: 01.06.2017 WEEK_OF_MONTH: 1
Date End: 30.06.2017 WEEK_OF_MONTH: 5
Date Start: 01.07.2017 WEEK_OF_MONTH: 0
Date End: 31.07.2017 WEEK_OF_MONTH: 5
Date Start: 01.08.2017 WEEK_OF_MONTH: 1
Date End: 31.08.2017 WEEK_OF_MONTH: 5
Date Start: 01.09.2017 WEEK_OF_MONTH: 0
Date End: 30.09.2017 WEEK_OF_MONTH: 4
Date Start: 01.10.2017 WEEK_OF_MONTH: 0
Date End: 31.10.2017 WEEK_OF_MONTH: 5
Date Start: 01.11.2017 WEEK_OF_MONTH: 1
Date End: 30.11.2017 WEEK_OF_MONTH: 5
Date Start: 01.12.2017 WEEK_OF_MONTH: 0
Date End: 31.12.2017 WEEK_OF_MONTH: 4
我们找不到任何记录的c在API 24中有Calendar
的变更。
任何线索如何解决此问题?
我们已经实现了我们自己的方法WEEK_OF_MONTH
返回与Android 6.x相同的值,但通过它的外观set(WEEK_OF_MONTH)
也被打破。 使用Joda重写代码不是一种选择。
这个固定我的问题,太棒了!谢谢 :) – TheMenace