[英]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: 解决您的问题的步骤:
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.