Android中的解析日期(DatabaseHelper.java)

时间:2022-10-29 20:24:51

I've got an annoying problem with my dates. I want to convert my string (which I get from a textView) into a date format. Sadly my parsing keeps throwing me errors.

我的日期有一个烦人的问题。我想将我的字符串(我从textView获取)转换为日期格式。可悲的是,我的解析不断给我带来错误。

This is the code:

这是代码:

public void submitExpenseButton_click(View v){
EditText editDate = (EditText) findViewById(R.id.dateNewExpense);
            String date = editDate.getText().toString();

Finance finance = new Finance();
finance.setDate(Date.valueOf(date));
db.insertIncome(finance);
}

Everytime I try to insert a record I get the following error in my errorlog: Android中的解析日期(DatabaseHelper.java)

每次我尝试插入记录时,我的错误日志中都会出现以下错误:

Could anybody tell me how to fix this? I'm out of clue's here. For further reference I've included my class, so you know how the getters and setters work too.

谁能告诉我如何解决这个问题?我不在乎这里的线索。为了进一步参考,我已经包括了我的课程,所以你也知道getter和setter是如何工作的。

finance.class:

public class Finance {
    private Date date;

public Finance(Date date) {
        this.date = date;
    }

public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

DatabaseHelper.java:

public long insertIncome(Finance finance) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("date", String.valueOf(finance.getDate()));
        long id = db.insert("finance", null, values);

        db.close();
        return id;
    }

NOTE: I stripped out the other fields! I do have id's etc in that db.. I left them out to reduce text. So what is the correct way for parsing / formatting / ..?

注意:我剥离了其他字段!我在那个db中有id等等。我把它们留下来减少文本。那么解析/格式化...的正确方法是什么?

Thanks Yenthe

1 个解决方案

#1


0  

I would suggest you use something like this, which is a standard way we convert dates in java:

我建议你使用这样的东西,这是我们在java中转换日期的标准方法:

String dateText = editDate.getText().toString()
Date date = new SimpleDateFormat("MMMM d, yyyy").parse(dateText);

Be sure to use the correct format string so it matches what is in your edit field.

请务必使用正确的格式字符串,以使其与编辑字段中的字符串匹配。

More detail here:

更多细节在这里:

http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

To convert the Date to a sql Date, which is a different object, just do this:

要将Date转换为sql Date,这是一个不同的对象,只需执行以下操作:

java.sql.Date sqlDate = new java.sql.Date(date.getTime());

#1


0  

I would suggest you use something like this, which is a standard way we convert dates in java:

我建议你使用这样的东西,这是我们在java中转换日期的标准方法:

String dateText = editDate.getText().toString()
Date date = new SimpleDateFormat("MMMM d, yyyy").parse(dateText);

Be sure to use the correct format string so it matches what is in your edit field.

请务必使用正确的格式字符串,以使其与编辑字段中的字符串匹配。

More detail here:

更多细节在这里:

http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

To convert the Date to a sql Date, which is a different object, just do this:

要将Date转换为sql Date,这是一个不同的对象,只需执行以下操作:

java.sql.Date sqlDate = new java.sql.Date(date.getTime());