在日期选择器对话框中仅显示当前和上个月。
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);
答案 0 :(得分:3)
在“时间戳记”中选择日期,请参见以下代码
Long tsLong = System.currentTimeMillis()
然后将tsLong
转换为任何特定格式,然后提取所需的格式。
答案 1 :(得分:1)
尝试此解决方案。从上个月的第一天到今天。
在科特林:
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();
答案 2 :(得分:0)
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);