Access problems for datetime type data in the Android SQLite

Source: Internet
Author: User

about TalkSQLiteThe date type data is accessed online quite a few, but it is found to be so thin. More is to useDate, do not do date data statistical analysis, the access requirements of the relatively small direct conversionLong, i.e.datetime.gettime ()storage. It needs to be displayed on the interface.Longthe date data of the model intoDateorDateTimetype. But if you want to do statistical analysis of the data so that every record to traverse the conversion will be particularly troublesome, the amount of data is large, time consumption is very large.

We know that date type data is in yyyy-mm-dd form in the database , and time is hh:mm form of storage. The DateTime type is stored in yyyy-mm-dd HH:mm:ss.fff, year,month, day, time, minute, second, and millisecond. How do you store data for DateTime types? I believe it's a problem that bothers beginners.

       String timestr = "2015-3-1 16:20";       SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");       Format.settimezone (Timezone.gettimezone ("gmt+08:00"));//Beijing, China Time, East eight zone       date dateTime = null;       try {        dateTime = (Date) format.parse (TIMESTR),       } catch (ParseException e) {        e.printstacktrace ();       }

Java.sql.Date is the data type that is set to match SQL Date . The "normalized" java.sql.Date contains only month and day information, because if the information in the denormalized part of the database exists, such as milliseconds or seconds, it will rob Zero.

To save The exact value of the java.util.Date,

We need to use java.sql.Timestamp .

Timestamp Timestamp = new Timestamp (Datetime.gettime ());

This allows you to Insert a DateTime type of data directly into SQLite with timestamp .

What about that? The key issue should appear in the cursor without a method that specifically takes date data.

Note that the data in theAndroid Sqlite is stored in a weak type. Taken as a String and converted to a 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);
Reprint Please specify the Source:http://blog.csdn.net/lindonglian/article/details/44003549     

Access Problems with datetime type data in SQLite for Android

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.