Question

Hi below is the code I am using to parse Date from database:-

 String myFormat = "yyyy-MM-dd HH:mm:ss"; 
             SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

                Date savedDate,currentDate;
                for (int i = 0; i < rem_lst.size(); i++)
                {
//                  Toast.makeText(this, ""+rem_lst.get(i).rem_date, Toast.LENGTH_LONG).show();
                    try
                    {
                        String str = rem_lst.get(i).rem_date;

                        //savedDate = sdf.parse(str);
                        savedDate = sdf.parse(str);
                        //currentDate = calendar.getTime();
                        currentDate = sdf.parse("2014/04/02 15:30:53");
//                      if(savedDate.compareTo(currentDate)>0){
//                          System.out.println("Date1 is after Date2");
//                      }
//                      if(savedDate.compareTo(currentDate)<0){
//                          System.out.println("Date1 is before Date2");
//                      }
                        if(savedDate.compareTo(currentDate)==0){
                            System.out.println("Date1 is equal to Date2");
                            showNotifacation();
                        }
                       System.out.println("Date1 is not equal to Date2");
                    }catch (ParseException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

But I am getting exception as unable to parse date.

Code to insert date is :-

str_Rem_Date    = editText_Date.getText().toString();
                str_Rem_Catagory = ma.GetItem(spinner_Rem_Catagory.getSelectedItemPosition());

                if(editText_Date.length()==0)
                {
                    showAlert();
                }
                else
                {
                    DBAdapter adapter = new DBAdapter(getActivity());
                    adapter.insertReminder(str_Rem_Date, str_Rem_Catagory);

                }

Will anyone please tell me how to resolve this error. Thanks in advance!

Était-ce utile?

La solution

Change this :

currentDate = sdf.parse("2014/04/02 15:30:53");

to this :

currentDate = sdf.parse("2014-04-02 15:30:53");

Autres conseils

you have to use the same Date format which is going to check with DB date.

String myFormat = "yyyy/MM/dd HH:mm:ss";

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top