1

I'm working on an android application and new to it.

I have to get date from user and then add 28 days and store it in database.

This is what I have done so far

private void saveDate() throws ParseException {
    DatabaseHelper db = new DatabaseHelper(ActivityPeriodToday.this.getActivity());

    String pDate = periodDate.getText().toString().trim();
    String pTime = periodTime.getText().toString().trim();
    String next_expected = getNextExpected(pDate);

    boolean isInserted = db.insertPeriodTodayIntoPeriods(pDate, pTime, early_late, pDifference, pType, next_expected);

    if (isInserted == true) {
        Toast.makeText(ActivityPeriodToday.this.getActivity(), "Saved", Toast.LENGTH_SHORT).show();

    } else {
        Toast.makeText(ActivityPeriodToday.this.getActivity(), "Could not be saved", Toast.LENGTH_SHORT).show();
    }

}

private String getNextExpected(String pDate) {
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Calendar c = Calendar.getInstance();
    try {
        c.setTime(sdf.parse(pDate));
    } catch (ParseException e) {
        e.printStackTrace();
    }

    c.add(Calendar.DAY_OF_MONTH, 28);
    return sdf.format(c.getTime());
}

But is code is not incrementing month.

Ex. If user selects 01/11/2016, then date is incremented and is saved 29/11/2016. But if user selects 16/11/2016 then saves date is 28/11/2016 but this should be 14/12/2016

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Anuj TBE
  • 9,198
  • 27
  • 136
  • 285
  • Why adding 28 to 01/11/2016 is 28/11/2016? Shouldn't it be 29/11/2016? – Marat Nov 16 '16 at 11:41
  • Are you sure you are getting the result wrong? Maybe you are checking wrong column or row? Because your code is ok and it is working. – Marat Nov 16 '16 at 11:44

4 Answers4

1

Try using this:

calendar.add(Calendar.DAY_OF_YEAR, 28);
MihaiV
  • 695
  • 6
  • 14
1

Step 1

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.setTime(sdf.parse(dateInString));

Step-2 use add() to add number of days to calendar

c.add(Calendar.DATE, 40); 
Victor
  • 4,171
  • 1
  • 29
  • 37
0

Its Working for me.

Calendar c = Calendar.getInstance();
            int Year = c.get(Calendar.YEAR);
            int Month = c.get(Calendar.MONTH);
            int Day = c.get(Calendar.DAY_OF_MONTH);
            //  current date
            String CurrentDate = Year + "/" + Month + "/" + Day;
            String dateInString = CurrentDate; // Start date
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            c = Calendar.getInstance();
            try {
                c.setTime(sdf.parse(dateInString));
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            c.add(Calendar.DATE, 28);//insert the number of days that you want
            sdf = new SimpleDateFormat("dd/MM/yyyy");
            Date resultdate = new Date(c.getTimeInMillis());
            dateInString = sdf.format(resultdate);
            Toast.makeText(MainActivity.this, ""+dateInString, Toast.LENGTH_SHORT).show();
Amol Nage
  • 85
  • 10
0

Your question may already have an answer here: How can I increment a date by one day in Java?

Or you can simply use

c.add(Calendar.DATE, 28);

instead of

c.add(Calendar.DAY_OF_MONTH, 28);
Community
  • 1
  • 1