0

I want interval date. Whatever user enter date from date picker and from this date I want to get after one month date Suppose 1 Aug 2014 -> Output will be 1 September 2014.Can someone help me .Thanks to appreciate.

Hare is my Activity code

{
// Get current date by calender
         final Calendar c = Calendar.getInstance();
         year  = c.get(Calendar.YEAR);
         month = c.get(Calendar.MONTH);
         day   = c.get(Calendar.DAY_OF_MONTH);

        // Month is 0 based, just add 1
         etReplacementDate.setText(new StringBuilder()
        .append(month + 1).append("-").append(day).append("-")
        .append(year).append(" "));

         etReplacementDate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                showDialog(DATE_OF_REPLACEMENT);
            }
        });

         String fixedDate = etReplacementDate.getText().toString().trim();

         SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
            Date convertedDate = new Date();
            try
            {
                convertedDate = dateFormat.parse(fixedDate);
            }
            catch (ParseException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("Date Consersion  = " + convertedDate);


     /****************ReplaceMent Date***************************************************/
         cal2.add(Calendar.getInstance(convertedDate), 30);
         Date date_30dayslater = cal2.getTime();
         System.out.println("date_30dayslater : " + date_30dayslater);


         /****************Interval Date***************************************************/
         String _30daysLater_String = new SimpleDateFormat("yyyy-MM-dd").format(date_30dayslater);
         etNextReplanishmentDate.setText(_30daysLater_String);
         System.out.println("30 days later: " + _30daysLater_String);
         System.out.println("______________________________________");

/****************Before Date***************************************************/
         cal2.add(Calendar.DATE, -1);
         Date beforDate = cal2.getTime();
         String beforDate_String = new SimpleDateFormat("yyyy-MM-dd").format(beforDate);
         System.out.println("beforDate_String: " + beforDate_String);     
}


@Override 
    protected Dialog onCreateDialog(int id)
    {
        switch (id) 
        {
        case DATE_OF_REPLACEMENT:return new DatePickerDialog(this, pickerListenerReplacement, year, month, day);
        }
        return null;
    }


    private DatePickerDialog.OnDateSetListener pickerListenerReplacement = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        @Override
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {

            year  = selectedYear;
            month = selectedMonth;
            day   = selectedDay;

            // Show selected date 
            etReplacementDate.setText(new StringBuilder().append(month + 1)
                    .append("-").append(day).append("-").append(year)
                    .append(" "));

           }
        };
        }
Robotics
  • 129
  • 13
  • What does one month mean? If i give 14 Aug 2014. What output you are expecting? – bhargavg Aug 14 '14 at 06:45
  • @bhargavg = The output will be = 14 Sept 2014 . But i don't want to use current date. Whatever i enter the date i want to get after one month date. – Robotics Aug 14 '14 at 06:48
  • @Robotics what if I give you 30 Jan 2014? – dcow Aug 14 '14 at 06:49
  • So, if I understand it correctly, you just want to increment the chosen month by 1 right? – bhargavg Aug 14 '14 at 06:50
  • @ bhargavg : Yah , you are absolutely right. Thats why I,m using this code cal2.add(Calendar.getInstance(convertedDate), 30); Date date_30dayslater = cal2.getTime(); System.out.println("date_30dayslater : " + date_30dayslater); – Robotics Aug 14 '14 at 06:55
  • @Robotics what is the current result above? – Rod_Algonquin Aug 14 '14 at 07:23
  • @ Rod_Algonquin : - Its getting error at the line of cal2.add(Calendar.getInstance(convertedDate), 30); = The method getInstance(Locale) in the type Calendar is not applicable for the arguments (Date) – Robotics Aug 14 '14 at 07:30
  • possible duplicate of [adding one month to current date?](http://stackoverflow.com/questions/4905416/adding-one-month-to-current-date) – Basil Bourque Aug 14 '14 at 10:31
  • @Basil Bourque :Month is update but day can not be changed .c2.add(Calendar.MONTH, 1); – Robotics Aug 14 '14 at 10:55

2 Answers2

2

What you want to do is add() one Calendar.MONTH to a date that you've gotten and parsed, etc. so I won't go into that. I'll assume you are handling everything correctly up to the point where you'd like to get the same day, if possible, of the next month.

Part of your problem, as your comment suggests, is Calendar.getInstance() does not have an implementation that takes a Date. But more importantly you don't need it. You have a Date and a Calendar instance and it seems like you're changing c2 anyway so why not use Calendar's setTime() method like this?

// setting c2 with the convertedDate then adding a month
c2.setTime(convertedDate);
c2.add(Calendar.MONTH, 1);

 // Simple example
 public static void main(String...args) {
    Date d = new Date();    
    Calendar c = Calendar.getInstance();
    c.setTime(d);
    System.out.println(c.getTime());
    c.add(Calendar.MONTH, 1);
    System.out.println(c.getTime());    
 }
ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
0

try to use this.

        Calendar c = Calendar.getInstance();
        int month = c.get((Calendar.MONTH));
        c.set(Calendar.MONTH, month + 1);
        long time = c.getTimeInMillis();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        String result = sdf.format(new Date(time1));
mrBatonec
  • 101
  • 1
  • 1