0

I have a table that has time column in yyyy-MM-dd:HH:mm format .I want to get the the time value that is nearest to current time and date.I am able to get the time value that is nearest to current time but not the date.

This is what I am doing

    SimpleDateFormat mSDFmSDF = new SimpleDateFormat("HH:mm");//yyyy-MM-dd:HH:mm not working
    Calendar calSet = Calendar.getInstance();
    TimeZone tz = TimeZone.getDefault();
    mSDF.setTimeZone(tz);
    String time = mSDF.format(calSet.getTime());
    Cursor selectedtime = mDB.rawQuery("SELECT " + ALARM_TIME + " FROM "
            + ALARM_TABLE + " WHERE " + ALARM_TIME + " >=  '" + time
            + "' ORDER BY " + ALARM_TIME + " LIMIT 1 ;", null); 

Every suggestions are welcomed please help me

Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64

1 Answers1

1

To perform arithmetic on date/time, SQLite provides JULIANDAY function which will convert your date into an FLOAT.

So,

SELECT ABS(JULIANDAY(SUBSTR(alarm_time,1,5)||' '||SUBSTR(alarm_time,-5))-JULIANDAY('NOW')), * FROM alarm_table;

will return time diferences (in days) from alarm_time to current time.

However, if you only need nearest time in future, simple comparison will suffice:

SELECT * FROM alarm_table WHERE alarm_time>=STRFTIME('%Y-%m-%d:%H:%M','NOW')
ORDER BY alarm_time LIMIT 1;
LS_ᴅᴇᴠ
  • 10,823
  • 1
  • 23
  • 46