我正在开发一个需要用户选择“从时间到”选项的应用程序,但是我无法像我想避免的那样,如果第一次大于第二次,就无法验证这两次。
@Override
public void onTimeSet(int id, TimePicker view, int hourOfDay, int minute) {
String AM_PM = " AM";
String mm_precede = "";
if (hourOfDay >= 12) {
AM_PM = " PM";
if (hourOfDay >=13 && hourOfDay < 24) {
hourOfDay -= 12;
}
else {
hourOfDay = 12;
}
} else if (hourOfDay == 0) {
hourOfDay = 12;
}
if (minute < 10) {
mm_precede = "0";
}
//Check the started and ended of the an appointment
sTimeApp.setText(hourOfDay + ":" + mm_precede + minute + AM_PM);
eTimeApp.setText(hourOfDay + ":" + mm_precede + minute + AM_PM);
if (sTimeApp.equals(eTimeApp)){
Toast.makeText(this, "Error , Time should be different ",Toast.LENGTH_SHORT).show();
return;
}else {
if (id == 3) {
if (minute <= 9) {
sTimeApp.setText(hourOfDay + ":" + mm_precede + minute + AM_PM);
}
} else if (id == 4) {
if (minute <= 9) {
eTimeApp.setText(hourOfDay + ":" + mm_precede + minute + AM_PM);
}
}
}
}
答案 0 :(得分:0)
我建议使用Joda Time-它更容易使用。只需将其包含在您的gradle.build项目中即可: 编译'joda-time:joda-time:2.9.4'
然后要确保时间更长,您可以执行以下操作(将两个日期都包装到各自的日期时间中):
//Note the +1 is required if using a calendar instance to get the month(it's 0-11, opposed to standard 1-12). If not, just remove.
DateTime startTime= new DateTime();
startTime.year().setCopy(theYear).monthOfYear().setCopy(theMonth + 1).dayOfMonth().setCopy(theDay).hourOfDay().setCopy(theHour).minuteOfHour(theMinute);
然后执行类似的操作:
if(startTime.getMilis() > endTime.getMillis())