最好的方式与日期在Android的SQLite的工作最好的、日期、方式、工作

2023-09-11 12:51:29 作者:Cripple(瘸子)

我有一些麻烦的日期我使用的SQLite的Andr​​oid应用程序的工作。 我有几个问题:

我应该SQLite中使用存储日期是什么类型(文本,整数,...)? 由于存储日期我怎么保存它正确地使用ContentValues​​最好的方法是什么? 什么是检索SQLite数据库中迄今为止最好的办法? 如何使在SQLite的SQL选择,按日期排序的结果? 解决方案

建议的做法是使用一个文本字段SQL精简版中存储日期。

存储日期UTC格式,默认的,如果你使用的日期时间('现在')(YYYY-MM-DD HH:MM:SS)将允许通过之日起列排序

检索日期从SQL精简版,你可以再格式/为使用日历或android.text.format.DateUtils.formatDateTime方法需要到当地区域化的格式转换。

字符串

下面是我用一个区域化格式化方法;

 公共静态字符串FORMATDATETIME(上下文的背景下,字符串timeToFormat){

    字符串finalDateTime =;

    SimpleDateFormat的iso8601Format =新的SimpleDateFormat(
            YYYY-MM-DD HH:MM:SS);

    日期日期= NULL;
    如果(timeToFormat!= NULL){
        尝试 {
            日期= iso8601Format.parse(timeToFormat);
        }赶上(ParseException的E){
            日期= NULL;
        }

        如果(日期!= NULL){
            时长= date.getTime();
            INT标志= 0;
            旗帜| = android.text.format.DateUtils.FORMAT_SHOW_TIME;
            旗帜| = android.text.format.DateUtils.FORMAT_SHOW_DATE;
            旗帜| = android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
            旗帜| = android.text.format.DateUtils.FORMAT_SHOW_YEAR;

            finalDateTime = android.text.format.DateUtils.formatDateTime(背景下,
            当+ TimeZone.getDefault()的getOffset(时),旗)。
        }
    }
    返回finalDateTime;
}
 

Android之SQLiteDatabase

I'm having some trouble working with dates on my Android application that uses SQLite. I have a couple questions:

What type should I use to store dates in SQLite (text, integer, ...)? Given the best way to store dates how do I store It properly using ContentValues? What's the best way to retrieve the date from the SQLite database? How to make a sql select on SQLite, ordering the results by date?

解决方案

It is recommended practice to use a text field to store dates within SQL lite.

Storing dates in UTC format, the default if you use datetime('now') (yyyy-MM-dd HH:mm:ss) will then allow sorting by the date column.

Retrieving dates as strings from SQL Lite you can then format/convert them as required into local regionalised formats using the Calendar or the android.text.format.DateUtils.formatDateTime method.

Here's a regionalised formatter method I use;

public static String formatDateTime(Context context, String timeToFormat) {

    String finalDateTime = "";          

    SimpleDateFormat iso8601Format = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");

    Date date = null;
    if (timeToFormat != null) {
        try {
            date = iso8601Format.parse(timeToFormat);
        } catch (ParseException e) {
            date = null;
        }

        if (date != null) {
            long when = date.getTime();
            int flags = 0;
            flags |= android.text.format.DateUtils.FORMAT_SHOW_TIME;
            flags |= android.text.format.DateUtils.FORMAT_SHOW_DATE;
            flags |= android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
            flags |= android.text.format.DateUtils.FORMAT_SHOW_YEAR;

            finalDateTime = android.text.format.DateUtils.formatDateTime(context,
            when + TimeZone.getDefault().getOffset(when), flags);               
        }
    }
    return finalDateTime;
}