如何从时间选择器对话框将最小时间设置为系统当前时间
How to set minimum time to system current time from timepickerdialog
我正在使用时间选择器对话框 select 时间,但我最近观察到当用户 select 时间时,它允许用户 select 已经过去的时间。
示例:如果当前日期是 25-03-2020 并且时间是 5:00PM 但时间选择器允许 select 时间低于 5:00PM.
那么我怎样才能提醒用户不要select超过当前日期的时间。
试试这个
fun validateStartDate(y: Int, m: Int, d: Int): Boolean {
val selectedDate = GregorianCalendar(y, m - 1, d).time
val currentTime = Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000)
val c = Calendar.getInstance()
Log.d("d", currentTime.toString())
c.time = currentTime
c.add(Calendar.YEAR, 1)
val newDate = c.time
Log.d("d", selectedDate.toString())
val check = selectedDate.before(currentTime) && selectedDate.before(newDate)
Log.d("D", check.toString())
return selectedDate.before(currentTime) && selectedDate.before(newDate)
}
编辑
Java
public boolean validateWarrantyDate(int y, int m, int d) {
Date selectedDate = new GregorianCalendar(y
, m - 1, d).getTime();
Date currentTime = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
Calendar c = Calendar.getInstance();
Log.d("d", String.valueOf(currentTime));
c.setTime(currentTime);
c.add(Calendar.YEAR, 1);
Date newDate = c.getTime();
Log.d("d", String.valueOf(newDate));
Log.d("d", String.valueOf(selectedDate));
boolean check = selectedDate.after(currentTime) && selectedDate.before(newDate);
Log.d("D", String.valueOf(check));
return selectedDate.after(currentTime) && selectedDate.before(newDate);
}
希望对您有所帮助。
您可以创建一个允许设置最短时间的自定义 timePicker,您可以试试这个解决方案
public class CustomTimePicker extends TimePickerDialog {
private int mMinHour = -1;
private int mMinMinute = -1;
private int mMaxHour = 100;
private int mMaxMinute = 100;
private int mCurrentHour;
private int mCurrentMinute;
public CustomTimePicker(Context context, OnTimeSetListener callBack, int hourOfDay,
int minute, boolean is24HourView) {
super(context, callBack, hourOfDay, minute, is24HourView);
mCurrentHour = hourOfDay;
mCurrentMinute = minute;
// Somehow the onTimeChangedListener is not set by TimePickerDialog
// in some Android Versions, so, Adding the listener using
// reflections
try {
Class<?> superclass = getClass().getSuperclass();
Field mTimePickerField = superclass.getDeclaredField("mTimePicker");
mTimePickerField.setAccessible(true);
TimePicker mTimePicker = (TimePicker) mTimePickerField.get(this);
mTimePicker.setOnTimeChangedListener(this);
} catch (NoSuchFieldException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
}
public void setMin(int hour, int minute) {
mMinHour = hour;
mMinMinute = minute;
}
public void setMax(int hour, int minute) {
mMaxHour = hour;
mMaxMinute = minute;
}
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
super.onTimeChanged(view, hourOfDay, minute);
boolean validTime;
if (((hourOfDay < mMinHour) || (hourOfDay == mMinHour && minute < mMinMinute))
|| ((hourOfDay > mMaxHour) || (hourOfDay == mMaxHour && minute > mMaxMinute))) {
validTime = false;
} else {
validTime = true;
}
if (validTime) {
mCurrentHour = hourOfDay;
mCurrentMinute = minute;
} else {
updateTime(mCurrentHour, mCurrentMinute);
}
}}
现在将 CustomTimePicker 用作:
CustomTimePicker customTimePicker = new CustomTimePicker(AddExtraForecast.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
int selected_hour = hourOfDay;
int selected_minutes = minute;
String min = "", hours = "";
if (selected_hour < 10)
hours = "0" + selected_hour;
else
hours = String.valueOf(selected_hour);
if (selected_minutes < 10)
min = "0" + selected_minutes;
else
min = String.valueOf(selected_minutes);
// Append in a StringBuilder
String aTime = new StringBuilder().append(hours).append(":")
.append(min).append(":").append("00").toString();
}, new Time(System.currentTimeMillis()).getHours(), new Time(System.currentTimeMillis()).getMinutes(), true);
customTimePicker.updateTime(// hours_in_integer, mins_in_integer));
customTimePicker.setMin(Integer.parseInt(// hours_in_integer, mins_in_integer));
customTimePicker.show();
感谢大家的帮助。但不知何故,我用以下方法解决了这个问题,所以我想这将有助于未来的研究人员获得解决方案。
首先,当用户从日期选择器中选择日期并设置日期并选择时间选择器时,它会从简单的日期格式生成一个日期,如果两个日期相同则匹配。如果是,则它开始使用另一种日期格式检查时间。
这是它的代码。我包括了日期选择器中的所有内容。
(Button_name_for_date_picker).setOnClickListener(view -> {
Calendar calendar1 = Calendar.getInstance();
int year = calendar1.get(Calendar.YEAR);
int month = calendar1.get(Calendar.MONTH);
int day = calendar1.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(OrderConfirmationActivity.this,
dateSetListener, year, month, day);
dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
dialog.getDatePicker().setMaxDate(System.currentTimeMillis() -1000 + (1000*60*60*24*7)); // This allows user to select a date range between a week
dialog.show();
});
dateSetListener = (view, year, month, dayOfMonth) -> {
month = month + 1;
if (month < 10){
String single_month = "0" + month;
(String_name) = dayOfMonth + "/" + single_month + "/" + year;
(Button_name).setText(AppointmentDate);
Appoint_date.setTextColor(getResources().getColor(android.R.color.holo_green_light, getTheme()));
}else{
AppointmentDate = dayOfMonth + "/" + month + "/" + year;
Appoint_date.setText(AppointmentDate);
Appoint_date.setTextColor(getResources().getColor(android.R.color.holo_green_light, getTheme()));
}
};
//This Generates AppointmentTime
Appoint_time.setOnClickListener(view -> {
Calendar calendar12 = Calendar.getInstance();
final int hour = calendar12.get(Calendar.HOUR_OF_DAY);
int minute = calendar12.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(OrderConfirmationActivity.this, (timePicker, selected_hour, selected_minute) -> {
SimpleDateFormat current_date = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
String check = current_date.format(new Date());
if (AppointmentDate.equals(check)){
SimpleDateFormat current_time = new SimpleDateFormat("HH", Locale.getDefault());
int check_hour = Integer.parseInt(current_time.format(new Date()));
String v = String.valueOf(future_time);
Toast.makeText(this, v, Toast.LENGTH_SHORT).show();
if (selected_hour < check_hour) {
future_time = 1;
String v1 = String.valueOf(future_time);
Toast.makeText(this, v1, Toast.LENGTH_SHORT).show();
}
}
if (selected_hour < 7){
time_check = 1;
}else if (selected_hour >= 19){
time_check = 1;
}
if (selected_hour > 12) {
AppointmentTime = (selected_hour - 12) + ":" + selected_minute + " " + "PM";
Appoint_time.setText(AppointmentTime);
} else if (selected_hour == 12) {
AppointmentTime = "12" + ":" + selected_minute + " " + "PM";
Appoint_time.setText(AppointmentTime);
} else {
if (selected_hour != 0) {
AppointmentTime = selected_hour + ":" + selected_minute + " " + "AM";
Appoint_time.setText(AppointmentTime);
} else {
AppointmentTime = "12" + ":" + selected_minute + " " + "AM";
Appoint_time.setText(AppointmentTime);
}
}
}, hour, minute, false);
timePickerDialog.show();
});
我正在使用时间选择器对话框 select 时间,但我最近观察到当用户 select 时间时,它允许用户 select 已经过去的时间。 示例:如果当前日期是 25-03-2020 并且时间是 5:00PM 但时间选择器允许 select 时间低于 5:00PM.
那么我怎样才能提醒用户不要select超过当前日期的时间。
试试这个
fun validateStartDate(y: Int, m: Int, d: Int): Boolean {
val selectedDate = GregorianCalendar(y, m - 1, d).time
val currentTime = Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000)
val c = Calendar.getInstance()
Log.d("d", currentTime.toString())
c.time = currentTime
c.add(Calendar.YEAR, 1)
val newDate = c.time
Log.d("d", selectedDate.toString())
val check = selectedDate.before(currentTime) && selectedDate.before(newDate)
Log.d("D", check.toString())
return selectedDate.before(currentTime) && selectedDate.before(newDate)
}
编辑 Java
public boolean validateWarrantyDate(int y, int m, int d) {
Date selectedDate = new GregorianCalendar(y
, m - 1, d).getTime();
Date currentTime = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
Calendar c = Calendar.getInstance();
Log.d("d", String.valueOf(currentTime));
c.setTime(currentTime);
c.add(Calendar.YEAR, 1);
Date newDate = c.getTime();
Log.d("d", String.valueOf(newDate));
Log.d("d", String.valueOf(selectedDate));
boolean check = selectedDate.after(currentTime) && selectedDate.before(newDate);
Log.d("D", String.valueOf(check));
return selectedDate.after(currentTime) && selectedDate.before(newDate);
}
希望对您有所帮助。
您可以创建一个允许设置最短时间的自定义 timePicker,您可以试试这个解决方案
public class CustomTimePicker extends TimePickerDialog {
private int mMinHour = -1;
private int mMinMinute = -1;
private int mMaxHour = 100;
private int mMaxMinute = 100;
private int mCurrentHour;
private int mCurrentMinute;
public CustomTimePicker(Context context, OnTimeSetListener callBack, int hourOfDay,
int minute, boolean is24HourView) {
super(context, callBack, hourOfDay, minute, is24HourView);
mCurrentHour = hourOfDay;
mCurrentMinute = minute;
// Somehow the onTimeChangedListener is not set by TimePickerDialog
// in some Android Versions, so, Adding the listener using
// reflections
try {
Class<?> superclass = getClass().getSuperclass();
Field mTimePickerField = superclass.getDeclaredField("mTimePicker");
mTimePickerField.setAccessible(true);
TimePicker mTimePicker = (TimePicker) mTimePickerField.get(this);
mTimePicker.setOnTimeChangedListener(this);
} catch (NoSuchFieldException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
}
public void setMin(int hour, int minute) {
mMinHour = hour;
mMinMinute = minute;
}
public void setMax(int hour, int minute) {
mMaxHour = hour;
mMaxMinute = minute;
}
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
super.onTimeChanged(view, hourOfDay, minute);
boolean validTime;
if (((hourOfDay < mMinHour) || (hourOfDay == mMinHour && minute < mMinMinute))
|| ((hourOfDay > mMaxHour) || (hourOfDay == mMaxHour && minute > mMaxMinute))) {
validTime = false;
} else {
validTime = true;
}
if (validTime) {
mCurrentHour = hourOfDay;
mCurrentMinute = minute;
} else {
updateTime(mCurrentHour, mCurrentMinute);
}
}}
现在将 CustomTimePicker 用作:
CustomTimePicker customTimePicker = new CustomTimePicker(AddExtraForecast.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
int selected_hour = hourOfDay;
int selected_minutes = minute;
String min = "", hours = "";
if (selected_hour < 10)
hours = "0" + selected_hour;
else
hours = String.valueOf(selected_hour);
if (selected_minutes < 10)
min = "0" + selected_minutes;
else
min = String.valueOf(selected_minutes);
// Append in a StringBuilder
String aTime = new StringBuilder().append(hours).append(":")
.append(min).append(":").append("00").toString();
}, new Time(System.currentTimeMillis()).getHours(), new Time(System.currentTimeMillis()).getMinutes(), true);
customTimePicker.updateTime(// hours_in_integer, mins_in_integer));
customTimePicker.setMin(Integer.parseInt(// hours_in_integer, mins_in_integer));
customTimePicker.show();
感谢大家的帮助。但不知何故,我用以下方法解决了这个问题,所以我想这将有助于未来的研究人员获得解决方案。
首先,当用户从日期选择器中选择日期并设置日期并选择时间选择器时,它会从简单的日期格式生成一个日期,如果两个日期相同则匹配。如果是,则它开始使用另一种日期格式检查时间。
这是它的代码。我包括了日期选择器中的所有内容。
(Button_name_for_date_picker).setOnClickListener(view -> {
Calendar calendar1 = Calendar.getInstance();
int year = calendar1.get(Calendar.YEAR);
int month = calendar1.get(Calendar.MONTH);
int day = calendar1.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(OrderConfirmationActivity.this,
dateSetListener, year, month, day);
dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
dialog.getDatePicker().setMaxDate(System.currentTimeMillis() -1000 + (1000*60*60*24*7)); // This allows user to select a date range between a week
dialog.show();
});
dateSetListener = (view, year, month, dayOfMonth) -> {
month = month + 1;
if (month < 10){
String single_month = "0" + month;
(String_name) = dayOfMonth + "/" + single_month + "/" + year;
(Button_name).setText(AppointmentDate);
Appoint_date.setTextColor(getResources().getColor(android.R.color.holo_green_light, getTheme()));
}else{
AppointmentDate = dayOfMonth + "/" + month + "/" + year;
Appoint_date.setText(AppointmentDate);
Appoint_date.setTextColor(getResources().getColor(android.R.color.holo_green_light, getTheme()));
}
};
//This Generates AppointmentTime
Appoint_time.setOnClickListener(view -> {
Calendar calendar12 = Calendar.getInstance();
final int hour = calendar12.get(Calendar.HOUR_OF_DAY);
int minute = calendar12.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(OrderConfirmationActivity.this, (timePicker, selected_hour, selected_minute) -> {
SimpleDateFormat current_date = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
String check = current_date.format(new Date());
if (AppointmentDate.equals(check)){
SimpleDateFormat current_time = new SimpleDateFormat("HH", Locale.getDefault());
int check_hour = Integer.parseInt(current_time.format(new Date()));
String v = String.valueOf(future_time);
Toast.makeText(this, v, Toast.LENGTH_SHORT).show();
if (selected_hour < check_hour) {
future_time = 1;
String v1 = String.valueOf(future_time);
Toast.makeText(this, v1, Toast.LENGTH_SHORT).show();
}
}
if (selected_hour < 7){
time_check = 1;
}else if (selected_hour >= 19){
time_check = 1;
}
if (selected_hour > 12) {
AppointmentTime = (selected_hour - 12) + ":" + selected_minute + " " + "PM";
Appoint_time.setText(AppointmentTime);
} else if (selected_hour == 12) {
AppointmentTime = "12" + ":" + selected_minute + " " + "PM";
Appoint_time.setText(AppointmentTime);
} else {
if (selected_hour != 0) {
AppointmentTime = selected_hour + ":" + selected_minute + " " + "AM";
Appoint_time.setText(AppointmentTime);
} else {
AppointmentTime = "12" + ":" + selected_minute + " " + "AM";
Appoint_time.setText(AppointmentTime);
}
}
}, hour, minute, false);
timePickerDialog.show();
});