Reputation: 139
I want to set alarm without datetime picker and time picker. It always shows invalid time. Here is my code
public void setClockTimeScheduler(Context ctx) {
// TODO Auto-generated method stub
Calendar current = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH),
cal.get(Calendar.HOUR),
cal.get(Calendar.MINUTE)+2, 00);
if(cal.compareTo(current) <= 0){
//The set Date/Time already passed
Toast.makeText(ctx, "Invalid Date/Time"+cal.compareTo(current)+"Or"+current,
Toast.LENGTH_LONG).show();
}else{
setAlarmScheduler(cal,ctx);
}
Upvotes: 1
Views: 292
Reputation: 1344
First of all you are adding two minutes in current time. if condition cannot be true at any cost. if statement is useless remove it and used the code below It will surely help you!
public void setClockTimeScheduler(Context ctx){
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE,2);
setAlarmScheduler(cal, ctx);
}
Upvotes: 1
Reputation: 3031
Are you sure, that you are running this code with the right parameters? Because I have tried your code and it worked for me (I mean I never get into the "Invalid Date/Time" block). I did some modification:
public void setClockTimeScheduler(Context ctx){
Calendar current = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MINUTE, current.get(Calendar.MINUTE)+2);
if(cal.compareTo(current) <= 0){
//The set Date/Time already passed
Log.d("MainActivity", "Invalid Date/Time" + cal.compareTo(current) + "Or" + current);
}else{
// setAlarmScheduler(cal, ctx);
}
}
Upvotes: 0