Update Calender Date According to set date

Hi i am developing a feature for warrenty period.There is one Text View and button.when user click on button calender open.User select date from calendar and this date will be set into that Text view. Now there is another one text view, one edit text and one spinner. Spinner have two value month and year.when user enter some numeric value like 13 and select month or year from spinner. second Text View should be update date.

I am trying to do this but if month added in edit text is more than 12, my concept goes wrong. Please help me.

if (selecteditem.equalsIgnoreCase(“Month”)) {

                String ed = edit_warrenty.getText().toString();
                SimpleDateFormat formatter = new SimpleDateFormat(
                        "dd-MM-yyyy");

                String dateInString = edit_date.getText().toString();
                Date date = null;
                try {

                    date = formatter.parse(dateInString);
                    System.out.println("===date===" + date);

                    String date1 = formatter.format(date);
                    System.out.println("===formateer.formatdate==" + date1);

                } catch (ParseException e) {
                    e.printStackTrace();
                } catch (java.text.ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                Calendar c = Calendar.getInstance();

                c.setTime(date);
                int month = c.get(Calendar.MONTH) + 1;
                int day = c.get(Calendar.DAY_OF_MONTH);
                int year = c.get(Calendar.YEAR);

                c.set(month, (month + Integer.parseInt(ed)));

                int newmonth = month + Integer.parseInt(ed);

                if (newmonth <= 12) {
                    c.set(newmonth, year, day);
                    tvwarrentyday
                            .setText(day + "-" + newmonth + "-" + year);
                } else {
                    int in = newmonth - 12;

                    int inn = month + in;
                    c.set(month, (month + in));
                    tvwarrentyday.setText(day + "-" + inn + "-" + year + 1);
                }

            }

            else if (selecteditem.equalsIgnoreCase("Year")) {
                String ed = edit_warrenty.getText().toString();
                SimpleDateFormat formatter = new SimpleDateFormat(
                        "dd-MM-yyyy");
                String dateInString = edit_date.getText().toString();

                Date date = null;
                try {

                    date = formatter.parse(dateInString);
                    System.out.println("===date===" + date);

                    String date1 = formatter.format(date);
                    System.out.println("===formateer.formatdate==" + date1);

                } catch (ParseException e) {
                    e.printStackTrace();
                } catch (java.text.ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                Calendar c = Calendar.getInstance();

                c.setTime(date);
                int month = c.get(Calendar.MONTH) + 1;
                int day = c.get(Calendar.DAY_OF_MONTH);
                int year = c.get(Calendar.YEAR);

                c.set(year, (year + Integer.parseInt(ed)));

                int newyear = year + Integer.parseInt(ed);
                c.set(newyear, year, day);
                tvwarrentyday.setText(day + "-" + month + "-" + newyear);
            } else {
                tvwarrentyday.setText("");
            }


Source: android

Leave a Reply