@@ -47,6 +47,10 @@ public class DatePickerDialog extends DialogFragment implements View.OnClickList
47
47
private HashSet <OnDateChangedListener > mListeners = new HashSet <OnDateChangedListener >();
48
48
private int mMaxYear = MAX_YEAR ;
49
49
private int mMinYear = MIN_YEAR ;
50
+ private int mStartMonth = 0 ;
51
+ private int mStartDay = 1 ;
52
+ private int mEndMonth = 11 ;
53
+ private int mEndDay = 31 ;
50
54
private LinearLayout mMonthAndDayView ;
51
55
private String mSelectDay ;
52
56
private String mSelectYear ;
@@ -67,6 +71,31 @@ private void adjustDayInMonthIfNeeded(int month, int year) {
67
71
if (currentDay > day )
68
72
this .mCalendar .set (Calendar .DAY_OF_MONTH , day );
69
73
}
74
+
75
+ private void adjustDayAndMonthIfNeeded (int month , int year ) {
76
+
77
+ if (year == this .mMinYear ) {
78
+ if (month < mStartMonth ) {
79
+ this .mCalendar .set (Calendar .MONTH , this .mStartMonth );
80
+ this .mCalendar .set (Calendar .DAY_OF_MONTH , this .mStartDay );
81
+ }
82
+ if (month == mStartMonth && this .mCalendar .get (Calendar .DAY_OF_MONTH ) < this .mStartDay ) {
83
+ this .mCalendar .set (Calendar .DAY_OF_MONTH , this .mStartDay );
84
+ }
85
+ }
86
+
87
+ if (year == this .mMaxYear ) {
88
+ if (month > mEndMonth ) {
89
+ this .mCalendar .set (Calendar .MONTH , this .mEndMonth );
90
+ this .mCalendar .set (Calendar .DAY_OF_MONTH , this .mEndDay );
91
+ }
92
+ if (month == mEndMonth && this .mCalendar .get (Calendar .DAY_OF_MONTH ) > this .mEndDay ) {
93
+ this .mCalendar .set (Calendar .DAY_OF_MONTH , this .mEndDay );
94
+ }
95
+ }
96
+ adjustDayInMonthIfNeeded (month , year );
97
+
98
+ }
70
99
71
100
public DatePickerDialog () {
72
101
// Empty constructor required for dialog fragment. DO NOT REMOVE
@@ -218,6 +247,10 @@ public View onCreateView(LayoutInflater layoutInflater, ViewGroup parent, Bundle
218
247
this .mWeekStart = bundle .getInt ("week_start" );
219
248
this .mMinYear = bundle .getInt ("year_start" );
220
249
this .mMaxYear = bundle .getInt ("year_end" );
250
+ this .mStartMonth = bundle .getInt ("month_start" );
251
+ this .mEndMonth = bundle .getInt ("month_end" );
252
+ this .mStartDay = bundle .getInt ("day_start" );
253
+ this .mEndDay = bundle .getInt ("day_end" );
221
254
currentView = bundle .getInt ("current_view" );
222
255
listPosition = bundle .getInt ("list_position" );
223
256
listPositionOffset = bundle .getInt ("list_position_offset" );
@@ -279,6 +312,10 @@ public void onSaveInstanceState(Bundle bundle) {
279
312
bundle .putInt ("week_start" , this .mWeekStart );
280
313
bundle .putInt ("year_start" , this .mMinYear );
281
314
bundle .putInt ("year_end" , this .mMaxYear );
315
+ bundle .putInt ("month_start" , this .mStartMonth );
316
+ bundle .putInt ("month_end" , this .mEndMonth );
317
+ bundle .putInt ("day_start" , this .mStartDay );
318
+ bundle .putInt ("day_end" , this .mEndDay );
282
319
bundle .putInt ("current_view" , this .mCurrentView );
283
320
int mostVisiblePosition = -1 ;
284
321
if (this .mCurrentView == 0 )
@@ -292,7 +329,7 @@ public void onSaveInstanceState(Bundle bundle) {
292
329
}
293
330
294
331
public void onYearSelected (int year ) {
295
- adjustDayInMonthIfNeeded (this .mCalendar .get (Calendar .MONTH ), year );
332
+ adjustDayAndMonthIfNeeded (this .mCalendar .get (Calendar .MONTH ), year );
296
333
this .mCalendar .set (Calendar .YEAR , year );
297
334
updatePickers ();
298
335
setCurrentView (0 );
@@ -316,8 +353,6 @@ public void setOnDateSetListener(OnDateSetListener onDateSetListener) {
316
353
}
317
354
318
355
public void setYearRange (int minYear , int maxYear ) {
319
- if (maxYear <= minYear )
320
- throw new IllegalArgumentException ("Year end must be larger than year start" );
321
356
if (maxYear > MAX_YEAR )
322
357
throw new IllegalArgumentException ("max year end must < " + MAX_YEAR );
323
358
if (minYear < MIN_YEAR )
@@ -327,7 +362,72 @@ public void setYearRange(int minYear, int maxYear) {
327
362
if (this .mDayPickerView != null )
328
363
this .mDayPickerView .onChange ();
329
364
}
365
+
366
+
367
+ /**
368
+ * Sets the start date for the DatePickerDialog.
369
+ *
370
+ * @param startYear The minimum year.
371
+ * @param startMonth The first valid month of the startYear. This must be a valid Calendar.Month. (0-11). Default is 0.
372
+ * @param startDay The first valid day of startMont (inclusive). Default is 1.
373
+ */
374
+ public void setStartDate (int startYear , int startMonth , int startDay ) {
375
+ setDateRange (startYear , startMonth , startDay , mMaxYear , mEndMonth , mEndDay );
376
+ }
377
+
378
+ /**
379
+ * Sets the end date for the DatePickerDialog.
380
+ *
381
+ * @param endYear The maximum year.
382
+ * @param endMonth The last valid month of the endYear. This must be a valid Calendar.Month. (0-11). Default is 11.
383
+ * @param endDay THe last valid day of endMonth (inclusive). Default is 31.
384
+ */
385
+ public void setEndDate (int endYear , int endMonth , int endDay ) {
386
+ setDateRange (mMinYear , mStartMonth , mStartDay , endYear , endMonth , endDay );
387
+ }
388
+
389
+ /**
390
+ * Sets the time period for the DatePickerDialog.
391
+ *
392
+ * @param startYear The minimum year.
393
+ * @param startMonth The first valid month of the startYear. This must be a valid Calendar.Month. (0-11). Default is 0.
394
+ * @param startDay The first valid day of startMont (inclusive). Default is 1.
395
+ * @param endYear The maximum year.
396
+ * @param endMonth The last valid month of the endYear. This must be a valid Calendar.Month. (0-11). Default is 11.
397
+ * @param endDay THe last valid day of endMonth (inclusive). Default is 31.
398
+ */
399
+ public void setDateRange (int startYear , int startMonth , int startDay , int endYear , int endMonth , int endDay ) {
400
+ setYearRange (startYear , endYear );
401
+ if (startMonth >= 12 || startMonth < 0 )
402
+ throw new IllegalArgumentException ("startMonth must be between 0-11" );
403
+ if (endMonth >= 12 || endMonth < 0 )
404
+ throw new IllegalArgumentException ("endMonth must be between 0-11" );
405
+ this .mStartMonth = startMonth ;
406
+ this .mStartDay = startDay ;
407
+ this .mEndMonth = endMonth ;
408
+ this .mEndDay = endDay ;
409
+ }
330
410
411
+ @ Override
412
+ public int getStartMonth () {
413
+ return this .mStartMonth ;
414
+ }
415
+
416
+ @ Override
417
+ public int getEndMonth () {
418
+ return this .mEndMonth ;
419
+ }
420
+
421
+ @ Override
422
+ public int getStartDay () {
423
+ return this .mStartDay ;
424
+ }
425
+
426
+ @ Override
427
+ public int getEndDay () {
428
+ return this .mEndDay ;
429
+ }
430
+
331
431
public void tryVibrate () {
332
432
if (this .mVibrator != null && this .mVibrate ) {
333
433
long timeInMillis = SystemClock .uptimeMillis ();
@@ -345,4 +445,5 @@ static abstract interface OnDateChangedListener {
345
445
public static abstract interface OnDateSetListener {
346
446
public abstract void onDateSet (DatePickerDialog datePickerDialog , int year , int month , int day );
347
447
}
448
+
348
449
}
0 commit comments