Попробуйте это решение.Это с первого дня предыдущего месяца по сегодняшний день.
В Kotlin:
private fun setupUi() {
// Get calendar instance
val calendar = Calendar.getInstance()
// Get current time
val currentYear = calendar.get(Calendar.YEAR)
val currentMonth = calendar.get(Calendar.MONTH)
val currentDay = calendar.get(Calendar.DAY_OF_MONTH)
// Create listener
val listener = DatePickerDialog.OnDateSetListener { view, year, month, day ->
// Show Toast after selection
Toast.makeText(this, "Selected: $day.$month.$year", Toast.LENGTH_SHORT).show()
}
// Max = current
val maxTime = calendar.timeInMillis
// Move day as first day of the month
calendar.set(Calendar.DAY_OF_MONTH, 1)
// Move "month" for previous one
calendar.add(Calendar.MONTH, -1)
// Min = time after changes
val minTime = calendar.timeInMillis
// Create dialog
val datePickerDialog = DatePickerDialog(
this,
listener,
currentYear,
currentMonth,
currentDay
).apply {
// Set dates
datePicker.maxDate = maxTime
datePicker.minDate = minTime
}
// Show dialog
datePickerDialog.show()
}
И на Java:
private void setupUi() {
// Get calendar instance
Calendar calendar = Calendar.getInstance();
// Get current time
int currentYear = calendar.get(Calendar.YEAR);
int currentMonth = calendar.get(Calendar.MONTH);
int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
// Create listener
DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
// Show Toast after selection
Toast.makeText(MainActivity.this, String.format("Selected: %s.%s.%s", day, month, year), Toast.LENGTH_SHORT).show();
}
};
// Max = current
long maxTime = calendar.getTimeInMillis();
// Move day as first day of the month
calendar.set(Calendar.DAY_OF_MONTH, 1);
// Move "month" for previous one
calendar.add(Calendar.MONTH, -1);
// Min = time after changes
long minTime = calendar.getTimeInMillis();
// Create dialog
DatePickerDialog datePickerDialog = new DatePickerDialog(
this,
listener,
currentYear,
currentMonth,
currentDay
);
// Set dates
datePickerDialog.getDatePicker().setMaxDate(maxTime);
datePickerDialog.getDatePicker().setMinDate(minTime);
// Show dialog
datePickerDialog.show();
}
Если вам нужен ВЕСЬ текущий месяц (с первого по последний день), вы можете заменить его на этот код Котлина :
// Move day as first day of the month
calendar.set(Calendar.DAY_OF_MONTH, 1)
// Move "month" for previous one
calendar.add(Calendar.MONTH, -1)
// Min = time after changes
val minTime = calendar.timeInMillis
// Move day as first day of the month
calendar.set(Calendar.DAY_OF_MONTH, 1)
// Move to next month
calendar.add(Calendar.MONTH, +2)
// Go back one day (so last day of current month)
calendar.add(Calendar.DAY_OF_MONTH, -1)
// Max = current
val maxTime = calendar.timeInMillis
и на Java:
// Move day as first day of the month
calendar.set(Calendar.DAY_OF_MONTH, 1);
// Move "month" for previous one
calendar.add(Calendar.MONTH, -1);
// Min = time after changes
long minTime = calendar.getTimeInMillis();
// Move day as first day of the month
calendar.set(Calendar.DAY_OF_MONTH, 1);
// Move to next month
calendar.add(Calendar.MONTH, +2);
// Go back one day (so last day of current month)
calendar.add(Calendar.DAY_OF_MONTH, -1);
// Max = current
long maxTime = calendar.getTimeInMillis();