【问题标题】:Date and Time Not inserting into the database in android application日期和时间未在 android 应用程序中插入数据库
【发布时间】:2018-05-30 16:34:30
【问题描述】:

我正在尝试将当前系统日期和时间作为条目插入表中。但它没有被插入。

它显示错误: “:05”附近:语法错误(代码1):,编译时:插入卡路里值(10, 21:05, 2018-05-30);

日期和日期时间函数的代码如下:

 String FetchTime()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:MM");
        String time = sdf.format(new Date());
            return time;
    }
    String FetchDate()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        return date;
    }

获取日期和时间并将它们放入表中的代码:

                time=FetchTime();
                date=FetchDate();
                System.out.println( time+"  <-->  "+date);
                String insert_stmt="INSERT into "+ msg.toString()+" values( "+ Integer.parseInt(msg1)+", "+time+" , " + date+");";
                SQLiteDatabase db=dbhelper.getWritableDatabase();
                db.execSQL(insert_stmt);

建表代码:

//create table
        SQLiteDatabase db=dbhelper.getWritableDatabase();
        String create_table="create table "+ txt.toString()+"(mvalue INTEGER, mtime varchar(20),mdate varchar(20) ); ";
        db.execSQL(create_table);

【问题讨论】:

  • SimpleDateFormat("HH:MM") 错误:M 代表月而不是分钟。改为“HH:mm”

标签: android database sqlite date time


【解决方案1】:

使用时间戳代替日期和时间

System.currentTimeMillis();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多