0

I want to get records from database for last 1 week. I take current date and the date 7 days before and I send them to the database handler but it returns error:

The method `getALLCompJobs(String, String)` in the type `DatabaseHandler` is not applicable for 
the `arguments (Date, Date)`

Below is my code. Please, help me to figure out how to get records for the past week from the database.

           public class JobSchema extends BackBaseActivity {

            Date dateBefore7day;
        Date CurrentDate;


         @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.jobsschema);
             Calendar c = Calendar.getInstance();

             SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy   HH:mm");
        df.format(c.getTime());
       CurrentDate=c.getTime();
               c.add(Calendar.DAY_OF_MONTH, -7);

              dateBefore7day=  c.getTime();



                    DatabaseHandler db = new DatabaseHandler(JobSchema.this);
            jobarr = db.getALLCompJobs(CurrentDate,dateBefore7day);


         <---error show  The method getALLCompJobs(String, String) in the type 
       DatabaseHandler is not applicable for the arguments (Date, Date) --?


        public class DatabaseHandler extends SQLiteOpenHelper {

              String CREATE_COMPLETED_TABLE = "CREATE TABLE " + TABLE_COMPLETED_JOBS
            + "(" + KEY_COMPID + " INTEGER PRIMARY KEY," + KEY_TIMEJOB
            + " TEXT," + KEY_TIMEWEEK+ " DATE,"    + KEY_PICK + " 
                          TEXT," + KEY_DEST + " TEXT,"
            + KEY_FARE + " TEXT" + ")";


                  public List<JobSchmeModel> getALLCompJobs(String CurrentDate, String 
          dateBefore7day) {
//public List<JobSchmeModel> getALLCompJobs() {
    List<JobSchmeModel> compjobsList = new ArrayList<JobSchmeModel>();

        String selectQuery = "SELECT  * FROM " + TABLE_COMPLETED_JOBS + " WHERE "  
      + KEY_TIMEWEEK + ">=" + dateBefore7day + " AND " + KEY_TIMEJOB + "<=" + 
       CurrentDate + " ORDER BY "+KEY_COMPID+" DESC";
            SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
Ivan Bartsov
  • 19,664
  • 7
  • 61
  • 59
  • you need to pass date as string in `getALLCompJobs(..)`. – Chirag Ghori Feb 26 '14 at 06:54
  • and how i will qeury for recoreds from database??? if i pass dates as string it will get records from database?? – user3332060 Feb 26 '14 at 06:55
  • yes, but make sure your date fields with data type `DATETIME` as i tell you last time if you remember. – Chirag Ghori Feb 26 '14 at 06:59
  • but in my database i m using only date for saving date in database check above – user3332060 Feb 26 '14 at 07:05
  • + KEY_TIMEWEEK+ " DATE," check this u mean change this to "DateTIME – user3332060 Feb 26 '14 at 07:05
  • Yes use `DATETIME` for both KEY_TIMEWEEK and KEY_TIMEJOB. – Chirag Ghori Feb 26 '14 at 07:09
  • I've posted an answer that should solve you issue. But consider this: this is not really the kind of issue you should ask help with on SO. This is basic, entry-level Java knowlede and simple attention to what compiler says. You absolutely have to learn some basics -- at least read a book on Java (e.g. _Thinking in Java_ by _Bruce Ekkel_ is a good one) -- before you ask questions on StackOverflow. – Ivan Bartsov Feb 26 '14 at 07:18

2 Answers2

0

If your language supports it, you can use a form of date called "epochtime" (number of seconds since 1 Jan, 1970). Simple arithmetic adding or subtracting seconds from the date in this form is very effective.

0

Well, read the error: it's telling you you're passing parameters of wrong type to your getALLCompJobs method. It takes String parameters, what you're passing it are of type Calendar. Try this

SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy   HH:mm");
String sCurrent = df.format(c.getTime());
c.add(Calendar.DAY_OF_MONTH, -7);
String sWeekBefore = df.format(c.getTime());
jobarr = db.getALLCompJobs(sCurrent, sWeekBefore);

UPD The format of the date string doesn't look right.

Check this answer for how to convert java.util.Date to java.sql.Date and don't forget to call .toString() on the date objects you pass to db.getALLCompJobs(...)

Community
  • 1
  • 1
Ivan Bartsov
  • 19,664
  • 7
  • 61
  • 59
  • I assumed you know your way around your sql. Is the sql query you use not giving you the needed entries? I'm no SQL expert, but I think it's the problem with your format string. Look at this answer for how to produce SQL-ready date object. http://stackoverflow.com/a/530022/375929 – Ivan Bartsov Feb 26 '14 at 07:25