android - Date string dose not get parse -


i have string in date format want parse in date object. want set time string in calendar object , want set alert @ same time.

for tried parse string date nothing happens. time dose not set.

when did debug check if time get's set in calendar or not, found goes directly @ end of method after parsing.

date date = df.parse(alertdatetest);

i tried parse string format :
simpledateformat df = new simpledateformat("d mmm yyyy");

but gives invalid format exception. format should use parse string?

string format : d mmm yyyy

code:

malertdate 25 apr 2016 alertdatetest 25 apr 2016

 public void updatenotification() {          try {               string alertdatetest = i.getstringextra("taskalertdate");             string alerttimetest = i.getstringextra("taskalerttime");              simpledateformat  df = new simpledateformat("yyyy-mm-dd");              date date = df.parse(alertdatetest);               if (alertdatetest.equals(malertdate)) {                   string formattedstring = date.tostring();                 calendar c = calendar.getinstance();                  c.set(calendar.hour_of_day, mhouroftheday);                 c.set(calendar.minute, mminute);                 c.set(calendar.second, 0);                  myear = new datetime(formattedstring).getyear();                 mmonth = new datetime(formattedstring).getmonthofyear();                 mdayofmonth = new datetime(formattedstring).getdayofmonth();                  c.set(calendar.year, myear);                 c.set(calendar.month, mmonth);                 c.set(calendar.day_of_month, mdayofmonth);                  date = c.gettime();                  log.d("alertdate", string.valueof(date));                 toast.maketext(addtaskactivity.this, string.valueof(date), toast.length_short).show();                  intent = new intent(addtaskactivity.this, notificationreceiver.class);                 pendingintent = pendingintent.getbroadcast(getbasecontext(), _mid, intent, 0);                 alarmmanager = (alarmmanager) getsystemservice(context.alarm_service);                 alarmmanager.set(alarmmanager.rtc_wakeup, date.gettime(), pendingintent);           } catch (parseexception e) {          }     } 

edit:

i have function ondateset of date picker in have string format , string formatted same format:

 @override     public void ondateset(datepickerdialog view, int year, int monthofyear, int dayofmonth) {      //   string date = "you picked following date: "+dayofmonth+"/"+(++monthofyear)+"/"+year;          simpledateformat  df = new simpledateformat("d mmm yyyy");         mcalendar = calendar.getinstance();         mcalendar.set(calendar.year, year);         mcalendar.set(calendar.month, monthofyear);         mcalendar.set(calendar.day_of_month, dayofmonth);          date date = mcalendar.gettime();           if(date_picker_tag == 1)         {             myear = year;             mmonth = monthofyear;             mdayofmonth = dayofmonth;              malertdate  = df.format(date);             alertdate.settext(string.valueof(malertdate));         }        if(date_picker_tag == 3) {              mduedate  = df.format(date);             duedate.settext(string.valueof(mduedate));         }     } 

edit: tried answer umesh saraswat

            string alertdatetest = i.getstringextra("taskalertdate");             string alerttimetest = i.getstringextra("taskalerttime");              simpledateformat simpledateformate = new simpledateformat("dd mmm yyyy");              date  date = simpledateformate.parse(alertdatetest);            //  date date = df.parse(alertdatetest);               if (alertdatetest.equals(malertdate)) {                   string formattedstring = date.tostring();                 calendar c = calendar.getinstance();                  c.set(calendar.hour_of_day, mhouroftheday);                 c.set(calendar.minute, mminute);                 c.set(calendar.second, 0);                  c.settime(date);                    c.set(calendar.year, myear);                 c.set(calendar.month, mmonth);                 c.set(calendar.day_of_month, mdayofmonth);                  date = c.gettime(); 

the date got wed dec 31. string 26 april 2016. time did not set.

thank you..

to parse date string 25 apr 2016 use dd mmm yyyy string format.

for more information visit, simpledateformat


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -