有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在安卓中以字符串和日期格式获取明天的日期,以便在sqlite查询中使用?

我试图删除s表中的一些记录,其中日期是今天的日期和明天的日期

public void deleteAll()
{
    // SQLiteDatabase db = this.getWritableDatabase();
    // db.delete(TABLE_NAME,null,null);
    SQLiteDatabase db = this.getWritableDatabase();

    // db.execSQL("delete  from "+ TABLE_NAME);
    // db.execSQL("TRUNCATE table" + TABLE_NAME);
    Date c = Calendar.getInstance().getTime(); System.out.println("Current time => " + c);
    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    String current_Date = df.format(c);

    Log.e("Current_Date=", current_Date);

    db.execSQL("DELETE FROM SchedledMeetings WHERE Meeting_Date = '''+current_Date+''' ");
    db.close();
}

但我希望使用Sqlite查询将其作为字符串和日期格式


共 (1) 个答案

  1. # 1 楼答案

    首先,我必须指出,以SQLite格式"dd/MM/yyyy"存储日期是错误的,因为这不是一种可比较的格式,所以当您希望在日期之间获取行时,无法轻松使用它进行选择。另外SQLite的日期函数无法识别此格式,因此您每次都可以对其进行转换
    最好使用这种格式:"yyyy-MM-dd"

    使用此代码:

    Calendar calendar = Calendar.getInstance();
    
    Date c = calendar.getTime();
    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    
    String current_Date = df.format(c);
    
    calendar.add(Calendar.DAY_OF_YEAR, 1);
    c = calendar.getTime();
    String tomorrow_Date = df.format(c);
    
    db.execSQL("DELETE FROM SchedledMeetings WHERE (Meeting_Date='" + current_Date + "') OR (Meeting_Date='" + tomorrow_Date + "')");
    db.close();
    

    请注意DELETE语句应该如下所示:

    db.execSQL("DELETE FROM SchedledMeetings WHERE (Meeting_Date='" + current_Date + "') OR (Meeting_Date='" + tomorrow_Date + "')");
    

    通过这种方式,可以传递current_Datetomorrow_Date的值,而不是字符串"current_Date""tomorrow_Date"
    这将删除包含当前日期和明天日期的所有行