如何仅显示当前月份并预览在 datePickerDialog 中可选择的月份日期?
How to show only current month and previews month's dates selectable in datePickerDialog?
在日期选择器对话框中仅显示当前月份和上个月。
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
转换为任何特定格式并提取您需要的格式。
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
试试这个解决方案。从上个月的第一天到今天
在 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();
}
如果您需要当月的整个月份(从第一天到最后一天),您可以用 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
并在 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 = 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
转换为任何特定格式并提取您需要的格式。
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
试试这个解决方案。从上个月的第一天到今天
在 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();
}
如果您需要当月的整个月份(从第一天到最后一天),您可以用 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
并在 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();