仅在日期选择器对话框中显示当前和上个月。
datePickerDialog = new DatePickerDialog(DateConvertActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
String s = "" + dayOfMonth + "-" + (month + 1) + "-" + year;
edtDate.setText(s);
}
}, mDay, mMonth, mYear);
datePickerDialog.getDatePicker().updateDate(mYear, mMonth, mDay);
在时间戳中选择日期请参见下面的代码
Long tsLong = System.currentTimeMillis()
然后将tsLong
转换为任何特定格式并提取所需的格式。
供参考:Android Timestamp
尝试此解决方案。从上个月的第一天到今天。
在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()
}
和in 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();
}
如果您需要整个当月(从第一天到最后一天),则可以用此 kotlin代码替换PAR:
// 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
和in 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();
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);