in this method I insert multiple records with start date and end date. That works fine, but for example if I choose the start date as 31/01/2014 and 31/07/2014 as the end date I choose. records of the following months will be placed always on the 28th because he takes on the last day of the month. How can I avoid this problem?
GregorianCalendar dateAndTime =(GregorianCalendar)GregorianCalendar.getInstance();
GregorianCalendar dateAndTime1 =(GregorianCalendar)GregorianCalendar.getInstance();
public void InsRecords(){
SQLiteDatabase db = mHelper.getWritableDatabase();
ContentValues cv =newContentValues();
if(mRadioGroup.getCheckedRadioButtonId()== R.id.radio){
SimpleDateFormat sdf1 =newSimpleDateFormat("yyyy-MM-dd");
while(dateAndTime.compareTo(dateAndTime1)<=0){
String strDate = sdf1.format(dateAndTime.getTime());
dateAndTime.add(Calendar.MONTH, 1);
cv.put(eTable.DATE, strDate);
db.insert(eTable.TABLE_NAME,null, cv);
}
db.close();
}
I'm not quite sure if I'm understanding completely, but I think you mean: if the starting date is the last day of the month, then the following dates should be as well. If so, then I believe this might work for you:
public void InsRecords()
{
SQLiteDatabase db = mHelper.getWritableDatabase();
ContentValues cv = newContentValues();
if (mRadioGroup.getCheckedRadioButtonId() == R.id.radio)
{
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
boolean lastDay = dateAndTime.get(GregorianCalendar.DAY_OF_MONTH) == dateAndTime.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
while (dateAndTime.compareTo(dateAndTime1) <= 0)
{
String strDate = sdf1.format(dateAndTime.getTime());
dateAndTime.add(Calendar.MONTH, 1);
if (lastDay)
{
dateAndTime.set(GregorianCalendar.DAY_OF_MONTH, dateAndTime.getActualMaximum(GregorianCalendar.DAY_OF_MONTH));
}
cv.put(eTable.DATE, strDate);
db.insert(eTable.TABLE_NAME, null, cv);
}
db.close();
}
}
For your example, this will give the following:
2014-01-31
2014-02-28
2014-03-31
2014-04-30
2014-05-31
2014-06-30
2014-07-31