DatePicker借阅归还日期计算算法错误的修复咨询
Hey there! Let's sort out that wonky return date calculation in your Borrow activity. Your current approach of manually checking day values is causing those messy date inconsistencies—let's fix this properly using built-in date handling tools instead of reinventing the wheel.
The Problem with Your Current Code
Your existing onDateSet logic has a few critical flaws:
- It only handles dates where the day is 27 or later, leaving all other dates without a calculated return date
- The manual calculation (
(i2+5)*0+1) doesn't actually compute the correct 5-day offset—it just arbitrarily sets the day to 1 - It ignores month length differences (e.g., 30th March +5 days should be 4th April, not a broken date)
- It doesn't account for year rollovers (e.g., 30th December +5 days should be 4th January next year)
The Solution: Use Calendar for Date Arithmetic
The Calendar class is designed to handle all these edge cases automatically. Here's how to rewrite your onDateSet method to correctly calculate the return date:
Modified onDateSet Method
@Override public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDay) { // Save the selected borrow date (keep your original logic here) yearFinal = selectedYear; monthFinal = selectedMonth + 1; // Convert to 1-based month for display dayFinal = selectedDay; tvScheduleDate.setText(dayFinal + "-" + monthFinal + "-" + yearFinal); // Calculate return date: selected date + 5 days Calendar returnCalendar = Calendar.getInstance(); // Set calendar to the selected borrow date (note: Calendar uses 0-based month) returnCalendar.set(selectedYear, selectedMonth, selectedDay); // Add 5 days—Calendar handles month/year rollovers automatically returnCalendar.add(Calendar.DAY_OF_MONTH, 5); // Extract return date components (convert month back to 1-based for display) int returnYear = returnCalendar.get(Calendar.YEAR); int returnMonth = returnCalendar.get(Calendar.MONTH) + 1; int returnDay = returnCalendar.get(Calendar.DAY_OF_MONTH); // Update the return date TextView tvReturnDate.setText(returnDay + "-" + returnMonth + "-" + returnYear); }
Key Improvements
- No manual date checks:
Calendar.add()takes care of every edge case—month changes, leap years, year transitions—so you don't have to write messy conditional logic - Separate variables: We don't overwrite your borrow date variables (
yearFinal,monthFinal,dayFinal), which is crucial if you need to save both dates to Firebase later - Consistent formatting: Both borrow and return dates use the same display logic, avoiding formatting mismatches
Bonus: Modern Date/Time API (Java 8+)
If your project targets API level 26+ or uses desugaring, you can use the cleaner LocalDate API instead of Calendar:
@Override public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDay) { // Save borrow date yearFinal = selectedYear; monthFinal = selectedMonth + 1; dayFinal = selectedDay; tvScheduleDate.setText(dayFinal + "-" + monthFinal + "-" + yearFinal); // Calculate return date with LocalDate LocalDate borrowDate = LocalDate.of(selectedYear, selectedMonth + 1, selectedDay); LocalDate returnDate = borrowDate.plusDays(5); // Update TextView tvReturnDate.setText(returnDate.getDayOfMonth() + "-" + returnDate.getMonthValue() + "-" + returnDate.getYear()); }
This API is more readable, less error-prone, and avoids the quirks of the older Calendar class.
内容的提问来源于stack exchange,提问作者Excalibur

