3

I am developing android application, in that application How to calculate the end date based on Start Date and No of days?E.g I have starting date Sep 13, 2012 if i add 45 days with that date I will get 27 October 2012.

How to do that?

I cannot use the date function inside if statement.

My code:

f (resCrop.equals("Paddy")) {

    if(rescultivation1.equals("Conventional method - Delta"))
        {
            if(resvariety1.equals("Short duration"))
            {
                WA.setVisibility(View.VISIBLE);
                TONE.setVisibility(View.VISIBLE);
                TTWO.setVisibility(View.VISIBLE);
                TTHREE.setVisibility(View.VISIBLE);
                //date calculation
                        SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy");
                Calendar c1 = Calendar.getInstance(); // Get Calendar Instance
            try {
                c1.setTime(sdf.parse(resDate));
                } catch (ParseException e) 
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                c1.add(Calendar.DATE, 3);  // add 3 days
                sdf = new SimpleDateFormat("dd-M-yyyy");

          Date resultdate = new Date(c1.getTimeInMillis());   // Get new time 
          String dateInString = sdf.format(resultdate);
          WP.setText(dateInString);
          WP.setEnabled(false);
          Calendar c2 = Calendar.getInstance();
          c2.add(Calendar.DATE, 35);  // add 45 days
          sdf = new SimpleDateFormat("dd-M-yyyy");
            }
        }
     }

Its not working inside the if statement. if i used outside if its working fine. how to resolve this problem. please advice me.

3 Answers3

9

Try this code . You can use any date format what you want.

    String dateInString = "2011-09-13";  // Start date
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    Calendar c = Calendar.getInstance(); // Get Calendar Instance
    c.setTime(sdf.parse(dateInString));

    c.add(Calendar.DATE, 45);  // add 45 days
    sdf = new SimpleDateFormat("MM/dd/yyyy");

    Date resultdate = new Date(c.getTimeInMillis());   // Get new time 
    dateInString = sdf.format(resultdate);
    System.out.println("String date:"+dateInString);
Chirag
  • 56,621
  • 29
  • 151
  • 198
  • Excellent. Thanks a lot chirag raval. its working fine. Thanks for your immediate answer. – Madhan Shanmugam Sep 13 '12 at 12:02
  • If i want to implement this calculation for more than 100 edit text. What can i do for it. I have to write this code for every edit text? – Madhan Shanmugam Sep 14 '12 at 00:58
  • @MadhanShanmugam No you have to make one function , in that you have to pass start date and number of days to add , that will returns the end date . So no need to write code for all the edittext . – Chirag Sep 14 '12 at 04:33
  • I already accepted your answer and also checked accepted symbol. Thanks chirag raval, i completed that task.Actually i am new to the android development. – Madhan Shanmugam Sep 14 '12 at 07:54
1

you can use the Below Code to Solve your Problem.

Calendar cal=Calendar.getInstance();
    int currentDay=cal.get(Calendar.DAY_OF_MONTH);
    //Set the date after 45 days 
    cal.set(Calendar.DAY_OF_MONTH, currentDay+45);
    int EndDay=cal.get(Calendar.DAY_OF_MONTH);
Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107
0

Hello Madhan,

          Plz Convert ur date into milli second and then differentiate it and divided it by 1000*60*60*24.

Please See the Code piece Like that

            Date d1 = new Date(1st Date);
            Date d2 = new Date(2nd Date);
            Calendar cal1 = Calendar.getInstance();
            cal1.setTime(d1);

            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(d2);
            presumedDays = (int) ((cal2.getTimeInMillis() - cal1.getTimeInMillis()) / (1000 * 60 * 60 * 24));

it give you remain days. You can Also follow this Click Here

AndroidDanger
  • 1,049
  • 1
  • 9
  • 17
  • He don't want remain days, he wants end date after adding some days to start date . – Chirag Sep 13 '12 at 11:37
  • 1
    Hello Chirag its very simple EndDate = Remainsdate+StartDate 1stly You convert the remainsdays in to milliseconds and then do this work. Means EndDate = Remainsdate+StartDate – AndroidDanger Sep 13 '12 at 12:10