2016-02-20 73 views
0

如何設置上個月的最短日期26.這是我的代碼。如何設置上個月的最短日期26

int difference = 0; 
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { 
    difference = 31; 
} else if (month == 4 || month == 6 || month == 9 || month == 11) { 
    difference = 30; 
} else if (month == 2) { 
    if (year % 4 == 0) { 
     difference = 29; 
    } else 
     difference = 28; 
} 

int minDiffrence = 0; 
if (day == 26) { 
    minDiffrence = difference; 
} else { 
    minDiffrence = difference - (26 - day); 
} 

dpd.getDatePicker().setMinDate(System.currentTimeMillis() - 24 * 60 * 60 * 1000 * minDiffrence); 
+0

在頂部,你說你想要上個月的第26個月,但你的代碼示例似乎是在這個月的最後一天。請澄清。你的問題不清楚。 –

回答

0

嘗試這種情況: -

Calendar calendar = Calendar.getInstance(); 

        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); 
        calendar.set(Calendar.DAY_OF_MONTH, 26); 

        dpd.getDatePicker().setMinDate(calendar.getTimeInMillis()); 
+0

它正在工作? – Sabari

0

此代碼示例代碼是覆蓋從日期=開始前一個月結束日期=當前月的當前日期的日期。我認爲這段代碼對你有幫助。

datePicker = (DatePicker) findViewById(R.id.datePicker); 

    calendar = Calendar.getInstance(); 
    calendar.add(Calendar.MONTH, -1); 
    calendar.set(Calendar.DAY_OF_MONTH, 1); 
    datePicker.setMinDate(calendar.getTimeInMillis()); 
    datePicker.setMaxDate(new Date().getTime()); 
相關問題