Android - SQLite - SELECT BETWEEN Date1 AND Date2

后端 未结 4 1466
暖寄归人
暖寄归人 2020-12-21 06:35

Mac OS-X

Android

Eclipse with ADT

SQLite

I\'m new to creating Android Apps and Ive researched this heavily but I\'m getting nowhere. I need

相关标签:
4条回答
  • 2020-12-21 07:13

    getting top two name of for 2 days

    Calendar cal = Calendar.getInstance();
                Date now = new Date();
                cal.setTime(now);
                String currentDate = sdf.format(cal.getTimeInMillis());
                cal.add(Calendar.DAY_OF_YEAR, -1);
                String previusdDate = sdf.format(cal.getTimeInMillis());
    
                Log.e("Start date", currentDate);
                Log.e("End date", previusdDate);
                SQLiteOpenHelper helper;
                SQLiteDatabase db;
                helper = new My_SQliteHelper(mContext, "MyDB", null, 1);
                db = helper.getWritableDatabase();
    
                Cursor cr = db.rawQuery("SELECT name,sum(minutes),date FROM Historyinfo WHERE date BETWEEN '" + previusdDate + "' AND '" + currentDate + "' GROUP BY name ORDER BY SUM(minutes) DESC LIMIT 2", null);
    

    100% working

    0 讨论(0)
  • 2020-12-21 07:25

    Ok, so I could not get string dates to work, so I had to convert String Dates to Calendar Dates to Unix Time before adding them to the SQLite database and convert them back (Unix Time to Calendar Dates to String) when displaying them. Unix Time allows calculations (order by, sort ascending, between etc) done on the date columns and it is the best method to use after long hours of trial and error. Here is the code I ended up using:

    Cursor c = newDB.rawQuery("select ID, Date, Hours from " + tableName + " where Date BETWEEN '" + startDateQueryDate + "' AND '" + endDateQueryDate + "' ORDER BY Date ASC", null);
    
                if (c != null ) {
                    if  (c.moveToFirst()) {
                        do {
                            int tempId = c.getInt(c.getColumnIndex("ID"));
                            long tempUnixTime = c.getLong(c.getColumnIndex("Date"));
    
                            //convert tempUnixTime to Date
                            java.util.Date startDateDate = new java.util.Date(tempUnixTime);
    
                            //create SimpleDateFormat formatter
                            SimpleDateFormat formatter1;
                            formatter1 = new SimpleDateFormat("dd/MM/yyyy", Locale.UK);
    
                            //convert Date to SimpleDateFormat and convert to String
                            String tempStringStartDate = formatter1.format(startDateDate);
    
                            int tempHours = c.getInt(c.getColumnIndex("Hours"));
                            results.add(+ tempId + "    Date: " + tempStringStartDate + "    Hours: " + tempHours);
                        }while (c.moveToNext());
                    }
                }
    
    0 讨论(0)
  • 2020-12-21 07:25

    Not sure where you read that Date / Time should be stored as strings !!! I dis-agree.

    I prefer storing Date / Times as INTEGER values. Read: http://www.sqlite.org/datatype3.html#datetime

    When Inserting data, convert your Date / time as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC. For e.g. 1st Jan 2012 00:00:00 GMT is represented as 1356998400000

    The Android Date / Calendar classes have built in functions to convert dates in UNIX Times and vice versa.

    Check : http://developer.android.com/reference/java/util/Date.html#getTime%28%29

    When selecting, you can use your normal select statements like BETWEEN or anything you prefer.

    I have been using this for many years and it works like a charm :D

    0 讨论(0)
  • 2020-12-21 07:33

    You must store date values in a format understood by SQLite. For plain dates, this would be a YYYY-MM-DD string, or a seconds value, or a Julian date number.

    To format a date as string, use something like this:

    Date date1 = ...;
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    String dateAsString = df.format(date1);
    

    To format a date as seconds value, divide the return value of Date.getTime() by 1000.


    If your date variables already are strings, you must ensure that they have the correct format.

    If your date strings do not have the yyyy-MM-dd format, SQLite's date functions will not understand them, and comparisons will not work (because the string must begin with the most significant field, the year, and all fields must have a fixed length, so that string comparisons come out correct).

    0 讨论(0)
提交回复
热议问题