0

this is my code and i am trying to retrieve the date of the sms that i have stored in my database, and pass it in an object to compare if the date has aged after 30 days and have a toast message that it expires.Thanks in advance for help

public class DataAging extends Activity implements Runnable {

private final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
private final long ONE_DAY = 24 * 60 * 60 * 1000;


  public void onCreate(Bundle savedInstanceState) {

      run();
}

  public String date;
  public long dbDate;

@Override
public void run() {

    ArrayList<Message> smsInbox = new ArrayList<Message>();

    String query_fetchSMS = "select * from " + Constants.table_sms + ";";
    smsDB smsD = new smsDB(this);
    SQLiteDatabase dbw = smsD.getWritableDatabase();
    Cursor cursor = dbw.rawQuery(query_fetchSMS, null);
    if (cursor != null) {
      cursor.moveToLast();
      if (cursor.getCount() > 0) {

        do {

          String time;

          time = (cursor.getString(cursor
              .getColumnIndex(Constants.columns_sms_date)));


          SimpleDateFormat dateFormat = new SimpleDateFormat("hmmaa"); 

          try {
              Date date = dateFormat.parse(time);

              Date now = new Date();
                long diff = now.getTime() - date.getTime();
                long days = diff / ONE_DAY;
                if(days > 1) { // More than 30 days?
                     // Expired !!!
                    Toast.makeText(context,"EXPIRED NA", Toast.LENGTH_SHORT).show();

            }

          } catch (ParseException e) {
          }

        } while (cursor.moveToPrevious());
      } 
    }

    dbw.close();
    smsD.close();
  }       
 }
Anand Dwivedi
  • 1,452
  • 13
  • 23

1 Answers1

0

You can filter the message based on date while retrieving sms datas from sqliteDatabase instead receiving entire datas from database.

try this one to get only more than 30 days sms datas from sqlitedatabase.

Calendar calendar= Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH,-30);
string oldMessageDate=formatter.format(calendar.getTime());
SQLiteDatabase dbw = smsD.getWritableDatabase(); 
Cursor   cursor=dbw.query(Constants.table_sms,null,Constants.columns_sms_date+"<?", new String[]{oldMessageDate},null,null,null);
if(cursor!=null){
if(cursor.moveToFirst()){
        do{
          Toast.makeText(context,"EXPIRED NA", Toast.LENGTH_SHORT).show();

        }while(cursor.moveToNext());
        cursor.close();
 }
}
Vishwa
  • 1,112
  • 1
  • 11
  • 23