简体   繁体   English

将字符串类型的日期保存为数据库作为日期类型

[英]Saving a date of type string into a database as type Date

I need to save a couple of dates into a database in Java. 我需要将几个日期保存到Java数据库中。 These dates are first received as strings. 这些日期首先作为字符串接收。 How do I convert them and save them as Date in the database? 如何转换它们并将它们另存为数据库中的日期? I was planning to use a STR_TO_DATE function, but how would the syntax for the query look like in that case? 我打算使用STR_TO_DATE函数,但是在这种情况下查询的语法如何?

This is what I have so far: 这是我到目前为止的内容:

public void addRow(Integer id, String infName, String infType,
    String domain, String language, String validType, Integer infoTypeId,
    String value, String effDate, String expDate, String processingTime)
    {
        String query="id+infName+language";
        if(query!=KEY)
        {
            try
            {
                                    // asking for a database
                SQLiteDatabase db=this.getWritableDatabase();
                ContentValues values=new ContentValues();

                values.put(PERSON_ID,id);
                values.put(INFO_NAME,infName);
                values.put(INFO_INPUT_TYPE,infType);
                values.put(INFO_DOMAIN, domain);
                values.put(INFO_LANGUAGE, language);
                values.put(INFO_REG_EX, validType);
                values.put(INFO_TYPE_ID,infoTypeId);
                values.put(INFO_VALUE, "NoValue");

// DATE-shouldnt be added as a string but as a DATE or TIMESTAMP
                values.put(INFO_EFFECTIVE_DATE, effDate);
// DATE-shouldnt be added as a string but as a DATE or TIMESTAMP
                values.put(INFO_EXPIRY_DATE, expDate);

                values.put(INFO_PROCESSING_TIME, processingTime);
                db.insert(TABLE_PROFILE,null,values);

                db.close();
            }
            catch(Exception e)
            {
                Toast.makeText(mContext,
                                        "Error in Inserting to Database",
                                         Toast.LENGTH_LONG).show(); 
            }
        }
    }

JodaTime非常适合在Java中处理日期

Steps to solve your issue: 解决您的问题的步骤:

  1. Add a language tag to your question. 在您的问题中添加语言标签。 Your code looks like Java, but I don't recognize Toast. 您的代码看起来像Java,但我不认识Toast。
  2. Google search for "format date in java". Google搜索“用Java格式化日期”。 A Google search is ALWAYS the best place to start when attempting to find a java solution (other solutions as well). 尝试查找Java解决方案(以及其他解决方案)时,Google搜索始终是最好的起点。
  3. Find, then read the SimpleDateFormat API Page ( JSE 6.0 SimpleDateFormat Page ). 查找,然后阅读SimpleDateFormat API页面( JSE 6.0 SimpleDateFormat页面 )。
  4. Write a test program to figure out how to use SimpleDateFormat. 编写测试程序以弄清楚如何使用SimpleDateFormat。
  5. Use SimpleDateFormat. 使用SimpleDateFormat。

Here is some example source: 这是一些示例源:

Date date = null;
final String dateValue = "12Nov2013";
final DateFormat dateFormat = new SimpleDateFormat("ddMMMyyyy");

try
{
    date = dateFormat.parse(dateValue);
    System.out.println("Parsed Date: " + date);
}
catch (ParseException exception)
{
    System.out.println("ParseException: " + exception);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM