Access to DateTime data in Android Sqlite
There are a lot of online accesses to date-type data in sqlite, but they are rarely mentioned in this detail. Date is used instead of Statistical Analysis of Date data, which is directly converted to long, that is, dateTime. getTime () storage. You need to convert long-type Date data to Date or DateTime type when it is displayed on the interface. However, if you want to perform statistical analysis, it will be especially troublesome to traverse and convert each record. If the data volume is large, the time consumption will be large.
We know that Date data is stored in the yyyy-MM-dd format in the database, while Time is saved in the HH: mm format. The DateTime type is stored in yyyy-MM-dd HH: mm: ss. fff, including year, month, day, hour, minute, second, and millisecond. In this way, how can DateTime data be stored? I believe it is a problem that troubles beginners.
String timeStr = "2015-3-1"; SimpleDateFormat format = new SimpleDateFormat (yyyy-MM-dd HH: mm: ss); format. setTimeZone (TimeZone. getTimeZone (GMT +); // Beijing time, UTC + dateTime = null; try {dateTime = (Date) format. parse (timeStr);} catch (ParseException e) {e. printStackTrace ();}
Java. SQL. Date is the data type set to match SQL DATE. The "normalized" java. SQL. Date only contains information about the year, month, and day. The reason is that information about the nonstandard part of the database, such as the hour, minute, and second, will be cleared in milliseconds.
To save the exact value of java. util. Date,
We need to use java. SQL. Timestamp
Timestamp timestamp = new Timestamp(dateTime.getTime());
In this way, you can directly use timestamp to insert DateTime data to sqlite.
So what? The key issue should be that there is no specific method to retrieve date data in the cursor.
Note that data in Android Sqlite is stored in a weak type. Take the String and convert it to the date type.
String str; SimpleDateFormat format; Date date = null; str = cursor. getString (cursor. getColumnIndex (corresponding column name); format = new SimpleDateFormat (yyyy-MM-dd HH: mm: ss); date = (Date) format. parse (str );