Click here to Skip to main content
16,012,468 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
date = formatter.parse(dateInString);
				time = (Date) formatterTime.parse(timeInString);
				Calendar current = Calendar.getInstance();

				Calendar cal = Calendar.getInstance();
				/*
				 * cal.set(DatePicker.g, pickerDate.getMonth(), pickerDate.getDayOfMonth(), pickerTime.getCurrentHour(),
				 * pickerTime.getCurrentMinute(), 00);
				 */
				cal.set(date.getYear(),date.getMonth(),date.getDay(),time.getHours(),time.getMinutes());//error i think so 

				if (cal.compareTo(current) <= 0)
				{
					// setAlarm(cal);
					// The set Date/Time already passed
					Toast.makeText(getApplicationContext(), "Invalid Date/Time", Toast.LENGTH_LONG).show();
				}
				else
				{
					 setAlarm(cal);
					 db.addContact(new Contact_details(name1, mobile_number1, home_number1, company_name1, jobtitle1, emailid1, date, time));
				}

if i set date and time greater also i am getting invalid date and time ?.can anyone say how to compare date and time
Posted
Updated 17-Sep-14 18:16pm
v2
Comments
Richard MacCutchan 15-Sep-14 12:49pm    
What do you mean by "comparison dnt take place"?
Are you saying the code never reached the compare statement?
Nandhini Devi 17-Sep-14 12:00pm    
ya ?
Richard MacCutchan 17-Sep-14 12:06pm    
Then there is some other code that you have not shown which causes the problem, or an exception is being caught somewhere and thrown away. I suggest you get together with your debugger and trace through the execution path.
Nandhini Devi 17-Sep-14 23:48pm    
date = formatter.parse(dateInString);
time = (Date) formatterTime.parse(timeInString);
Calendar current = Calendar.getInstance();

Calendar cal = Calendar.getInstance();
/*
* cal.set(DatePicker.g, pickerDate.getMonth(), pickerDate.getDayOfMonth(), pickerTime.getCurrentHour(),
* pickerTime.getCurrentMinute(), 00);
*/
cal.set(date.getYear(),date.getMonth(),date.getDay(),time.getHours(),time.getMinutes());

if (cal.compareTo(current) <= 0)
{
// setAlarm(cal);
// The set Date/Time already passed
Toast.makeText(getApplicationContext(), "Invalid Date/Time", Toast.LENGTH_LONG).show();
}
else
{
setAlarm(cal);
db.addContact(new Contact_details(name1, mobile_number1, home_number1, company_name1, jobtitle1, emailid1, date, time));
}
This is the code i am using inside try catch and i am not getting any exception
Richard MacCutchan 18-Sep-14 3:00am    
OK so you are not getting an exception, but what are you getting? Have you used your debugger to trace through the code and see whare it is going?

1 solution

Your problem is that two of your method calls do not return the values that you think, as described in the documentation[^].
getYear returns the year since 1900, i.e. 114 for 2014.
getDay returns the day of the week, not the day of the month.
Your code should read:
Java
Calendar cal = Calendar.getInstance();
cal.set(date.getYear()+1900,date.getMonth(),date.getDate(),time.getHours(),time.getMinutes());

Although note that all these methods are now deprecated.

So when you said that comparison didn't take place, that was not quite true; it did take place, but always produced the wrong result.
 
Share this answer
 
Comments
Nandhini Devi 18-Sep-14 6:50am    
i am getting date value from date picker and i changed to this format "dd-MM-yyyy" and time from time picker and changed the format to "HH:MM".now i want to compare the got date and time with current date and time.If the got date and time is higher than current date and time then insertion should takes place
Richard MacCutchan 18-Sep-14 7:19am    
I have explained above what was wrong with your code, you now need to change it to use the correct values.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900