我在编辑文本中有两个单独的时间选择器,用于开始时间和结束时间。当我选择开始时间为9:00时:在选择结束时间之后,它不会比开始时间短。结束时间应大于开始时间。建议一些解决方案。
int hour=0;
int minute=0;
TimePickerDialog mTimePicker;
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTimePicker = new TimePickerDialog(Jobcompletion.this, new TimePickerDialog.OnTimeSetListener() {
@SuppressLint("SetTextI18n")
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int selectedMinute) {
String AM_PM = " AM";
String mm_precede = "";
String hh_precede = "";
if (hourOfDay >= 12) {
AM_PM = " PM";
if (hourOfDay >= 13 && hourOfDay < 24) {
hourOfDay -= 12;
} else {
hourOfDay = 12;
}
} else if (hourOfDay == 0) {
hourOfDay = 12;
}
if (hourOfDay < 10) {
hh_precede = "0";
}
if (selectedMinute < 10) {
mm_precede = "0";
}
start.setText(hh_precede+hourOfDay + ":" +mm_precede + selectedMinute+ " " + AM_PM);
}
}, hour, minute, false); //12hour=false/true
mTimePicker.setTitle("Select StartTime");
mTimePicker.show();
}
});
end.setOnClickListener(new View.OnClickListener() {
/* Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);*/
@Override
public void onClick(View v) {
mTimePicker = new TimePickerDialog(Jobcompletion.this, new TimePickerDialog.OnTimeSetListener() {
@SuppressLint("SetTextI18n")
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int selectedMinute ) {
String AM_PM = " AM";
String mm_precede = "";
String hh_precede = "";
if (hourOfDay >= 12) {
AM_PM = " PM";
if (hourOfDay >= 13 && hourOfDay < 24) {
hourOfDay -= 12;
} else {
hourOfDay = 12;
}
} else if (hourOfDay == 0) {
hourOfDay = 12;
}
if (hourOfDay < 10) {
hh_precede = "0";
}
if (selectedMinute < 10) {
mm_precede = "0";
}
end.setText(hh_precede + hourOfDay + ":" + mm_precede + selectedMinute + " " + AM_PM);
}
}, hour, minute, false);
Log.e("Min",String.valueOf(minute));
mTimePicker.setTitle("Select EndTime");
mTimePicker.show();
}
});
答案 0 :(得分:1)
首先,如果您在应用中进行任何日期/时间操作,我强烈建议您导入Java 310 Backport for Android (ThreeTenABP):
compile 'com.jakewharton.threetenabp:threetenabp:1.0.3'
由于Java 7提供的Date
和Calendar
类很乱。
您需要自己实施验证。一个简单的实现是保持开始和结束时间的值,然后检查一个是否大于另一个。此代码假定使用310反向端口:
private LocalTime mStartTime;
private LocalTime mEndTime;
@Override
public void onTimeSet(TimePicker picker, int hourOfDay, int minuteOfHour) {
mStartTime = LocalTime.of(hourOfDay, minuteOfHour);
if(mStartTime.isAfter(mEndTime)) {
// Handle case
}
}
最后,您没有理由自己设置日期/时间文本。 Android为此提供了DateUtils
类:
long start = ZonedDateTime.of(LocalDate.now(), mStartTime, ZoneId.systemDefault()).toInstant().toEpochMilli();
String startTime = DateUtils.formatDateTime(this, start, DateUtils.FORMAT_SHOW_TIME);
这将考虑用户设置(12小时/ 24小时)和设备的区域设置。
以下是使用Java 7类的相同代码:
private Date mStartTime;
private Date mEndTime;
@Override
public void onTimeSet(TimePicker picker, int hourOfDay, int minuteOfHour) {
Calendar date = new GregorianCalendar();
date.set(Calendar.HOUR_OF_DAY, hourOfDay);
date.set(Calendar.MINUTE, minuteOfHour);
mStartTime = date.getTime();
if(mStartTime.after(mEndTime)) {
// Handle case
}
}
String startTime = DateUtils.formatDateTime(this, mStartTime.getTime(), DateUtils.FORMAT_SHOW_TIME);