date = formatter.parse(dateInString);if i set date and time greater also i am getting invalid date and time ?.can anyone say how to compare date and time
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));
}
Loading
Wim SturkenboomPosted Sep 19, 2014, 9:20 AM
Did you follow my suggestion about the breakpoint?
Nandhini JayachandranPosted Sep 19, 2014, 4:10 AM
{
//Date picker
if (v == setdate)
{
// Process to get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
dpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
// Display Selected date in textbox
setdate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dpd.show();
}
if (v == settime)
{
// Process to get Current Time
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog tpd = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener()
{
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
// Display Selected time in textbox
settime.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
tpd.show();
}
if (v == save_contacts)
{
final String name1 = name.getText().toString();
final String mobile_number1 = mobile_number.getText().toString();
final String home_number1 = home_number.getText().toString();
final String company_name1 = company_name.getText().toString();
final String jobtitle1 = jobtitle.getText().toString();
final String emailid1 = emailid.getText().toString();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
// SimpleDateFormat formatterDate = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat formatterTime = new SimpleDateFormat("HH:mm");
String dateInString = setdate.getText().toString();
String timeInString = settime.getText().toString();
Date date = null;
Date time = null;
Date todayDate;
try
{
date = formatter.parse(dateInString);
time = (Date) formatterTime.parse(timeInString);
//// current_date=formatter.parse()
//Calendar current = Calendar.getInstance();
//
todayDate = formatter.parse(formatter.format(new Date() ));
/*Calendar cal = Calendar.getInstance();
cal.set(date.getYear()+1900, date.getMonth(), date.getDay(),time.getHours(), time.getMinutes());
*/
if (date.compareTo(todayDate) <= 0)
{
// setAlarm(cal);
// The set Date/Time already passed
// setAlarm(cal);
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));
Toast.makeText(getApplicationContext(), "" + date + "" + time, Toast.LENGTH_LONG).show();
}
}
catch (java.text.ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
this is full code and i will get date and time using pickers and i need to compare with current date and time
Wim SturkenboomPosted Sep 19, 2014, 1:41 AM
'current' and 'cal' will follow each other; if 'current' changes, 'cal' changes and if 'cal' changes, 'current' changes.
I suggest that you put a breakpoint on if(cal.CompareTo(current) <=0) and and check cal and current. If I'm right, they will still be the same after you changed cal.
Below a simple example (I could not find a Calendar control so this is a bit of a guess how to get the info out of the Calendar control)
DateTime current = new DateTime(current.year, current.month, current.day);
DateTime cal = new DateTime(date.getYear(), date.getMonth(), date.getDay());
if (cal <= current)
{
}
This only takes date into account, not time. You can add the rest.