Reputation: 298
I'm using a time picker dialog to select time but I lately observed that when user selects time it allows user to select the time that is already passed. Example: If the current date is 25-03-2020 and time is 5:00PM but time picker allows to select time below 5:00PM.
So how can I alert user to not select passed time of current date.
Upvotes: 0
Views: 2417
Reputation: 298
Thanks for everyone's help. But somehow i solved the issue with the following approach so i guess that will help future researchers to get a solution.
At first when user picks date from the date picker and sets date and selects time picker then it generates a date from simple date format and matches if the both dates are same. if yes then it starts check the time with another dateformat.
here is the code of it. I included everything from date picker.
(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();
});
Upvotes: 0
Reputation: 961
try this
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)
}
Edit with 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);
}
I hope it will help you .
Upvotes: 1
Reputation: 308
You can create a custom timePicker which allow to set minimum time, you can try this solution
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);
}
}}
Now use the CustomTimePicker as :
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();
Upvotes: 1