Access to DateTime data in Android Sqlite

Source: Internet
Author: User

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 );
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.