我想实现以下行为:
less than 15
时,日期选择器中启用1-15
日期greater than 15
,日期选择器中启用了16-31
日期我的代码是:
private void DateDialog() {
final DatePickerDialog dpDialog = new DatePickerDialog(this,android.app.AlertDialog.THEME_DEVICE_DEFAULT_DARK,new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
//int curyear = cal.get(Calendar.YEAR);
cal.set(i, i1, i2);
etsubmstartdate.setText(dateformatter.format(cal.getTime()));
selecteddate = dateformatter.format(cal.getTime());
Log.e("Date of first edit text",selecteddate);
etsubmstartdate.setText(selecteddate);
}
}, year, month, day);
if (System.currentTimeMillis()<=15)
{
dpDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
long now = System.currentTimeMillis() - 1000;
dpDialog.getDatePicker().setMaxDate(now+(1000*60*60*24*15));
dpDialog.show();
}
else if(System.currentTimeMillis()>=15)
{
dpDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000*60*60*24*2);
long now = System.currentTimeMillis() - 1000*60*60*24*2;
dpDialog.getDatePicker().setMaxDate(now+(1000*60*60*24*14));
dpDialog.show();
}
}
答案 0 :(得分:0)
您可以使用日期类的 .before 和 .after 方法。
例如,
if (currentDate.before(yourdate)) {
//write your code require to before current date
}
if (currentDate.after(yourdate)) {
//write your code require to after current date
}
根据您的代码,请在 DateDialog()方法
中替换以下代码private void DateDialog() {
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
DatePickerDialog dpDialog = new DatePickerDialog(MainActivity.this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
Toast.makeText(MainActivity.this, "date::"+myCalendar.getTimeInMillis(), Toast.LENGTH_SHORT).show();
String dt = "2018-04-15";
String dtSixteen = "2018-04-16";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
try {
Date dateFifteen = format.parse(dt);
Calendar calFifteen = Calendar.getInstance();
calFifteen.setTime(dateFifteen);
Date dateSixteen = format.parse(dtSixteen);
Calendar calSixteen = Calendar.getInstance();
calSixteen.setTime(dateSixteen);
Date currentDate = myCalendar.getTime();
Calendar calendar = Calendar.getInstance();
calendar.setTime( myCalendar.getTime() );
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
currentDate = calendar.getTime();
if(currentDate.before(dateFifteen) || currentDate.equals(dateFifteen)){
dpDialog.getDatePicker().setMaxDate(calFifteen.getTimeInMillis());
}else {
dpDialog.getDatePicker().setMinDate(calSixteen.getTimeInMillis());
}
} catch (ParseException e) {
e.printStackTrace();
}
dpDialog.show();
}
答案 1 :(得分:0)
我没有测试过,但有类似的情况。
ZoneId zone = ZoneId.systemDefault();
LocalDate today = LocalDate.now(zone);
LocalDate min;
LocalDate max;
if (today.getDayOfMonth() < 16) {
min = today.withDayOfMonth(1);
max = today.withDayOfMonth(15);
} else {
min = today.withDayOfMonth(16);
max = today.with(TemporalAdjusters.lastDayOfMonth());
}
DatePicker picker = dpDialog.getDatePicker();
picker.setMinDate(min.atStartOfDay(zone).toInstant().toEpochMilli());
picker.setMaxDate(max.atStartOfDay(zone).toInstant().toEpochMilli());
dpDialog.show();
我使用的是现代Java日期和时间API java.time
。当你在这里时,你也可以用现代课程代替古老的Calendar
班级和朋友。现代API可以更好地使用。
我在通过ZoneId.systemDefault()
使用设备时区设置时犹豫不决,但由于日期选择器依赖它,我找不到任何方法。该程序的其他部分可能随时更改该设置,从而导致不可预测的结果。
将System.currentTimeMillis()
与值15进行比较将不起作用。该方法给出了自1970年1月15日00:00 UTC时代以来的毫秒数。目前的价值约为1 524 000 000 000.因此总是大于15。
你的时间数学是否正确我没有检查过。我觉得在代码中自己做这样的数学非常麻烦。我更喜欢把它留给经过充分测试的库方法。它更容易出错,它允许更清晰的代码。
是的,java.time
适用于较旧和较新的Android设备。它只需要至少Java 6 。
org.threeten.bp
导入日期和时间类。java.time
。java.time
。java.time
向Java 6和7的后端(JST-310的ThreeTen)。