简体   繁体   English

如何使用YYYY-MM-DD格式让SQLite按日期排序?

[英]How can i get SQLite to sort by date properly using YYYY-MM-DD format?

I'm making an app that needs to sort a Sqlite table of weeks by date and display just the date and the total hours worked that week in a listview. 我正在制作一个应用程序,需要按日期对Sqlite表进行排序,并在列表视图中显示该周的日期和总工时。

My activity extends listview and uses SimpleCursorAdapter to populate the listview. 我的活动扩展了listview,并使用SimpleCursorAdapter填充列表视图。 My date column is a TEXT field formatted like this "YYYY-MM-DD" 我的日期列是一个TEXT字段,格式为“YYYY-MM-DD”

Here is my database query function, I base my database helper class off of the google notepad example. 这是我的数据库查询功能,我的数据库助手类基于google记事本示例。

public Cursor getAllWeeks()
    {
        Cursor mCursor = mDb.query(true, DATABASE_TABLE, null, null, null, null, null, "weekdate ASC", null);


        if (mCursor != null)
        {
            mCursor.moveToFirst();
        }

        return mCursor;
    }

And I hook it to the ListView like this: 我把它挂钩到ListView,如下所示:

public class WeekList extends ListActivity implements OnClickListener{
    final static int DELETE_ID = Menu.FIRST + 1;

    public static NotesDbAdapter DBadapter;
    public static Cursor ListCursor;

    static final String[] displaycolumns = {"weekdate", "weektotalhours"};
    static final int[] listitemviews = {R.id.TextViewListItem1, R.id.TextViewListItem2};

    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.weeklist);
        Log.i("bendebug", "logtest");

        DBadapter = new NotesDbAdapter(this);
        DBadapter.open();
        ListCursor = DBadapter.getAllWeeks();


        this.setListAdapter(new SimpleCursorAdapter(this, R.layout.listitemlayouts, ListCursor, 
                displaycolumns, listitemviews));
        findViewById(R.id.ButtonAddWeek).setOnClickListener(this);

        //Activate the context menu
        registerForContextMenu(getListView());

    }

The problem I am having is that my cursor doesn't seem sorted properly, my weeklist looks like this: 我遇到的问题是我的光标似乎没有正确排序,我的周列表如下所示:

2010-7-19
2010-7-23
2010-7-24
2010-7-6

Instead of being properly sorted. 而不是正确排序。 I'm probably missing something really obvious, since I'm a total noob trying to teach myself. 我可能错过了一些非常明显的东西,因为我是一个试图自学的总菜鸟。

Obviously i shortened the code to make it more readable, since the rest of the activity is irrelevant AFAIK but I would be happy to post the complete code for the activity on request, it's really basic anyway. 显然我缩短了代码以使其更具可读性,因为活动的其余部分与AFAIK无关,但我很乐意根据请求发布活动的完整代码,无论如何它都是基本的。

使用前导零来表示月份和日期。

SQLite does not have built-in date fields; SQLite没有内置日期字段; you have to use TEXT (textual representation), INTEGER (unix time) or REAL. 你必须使用TEXT (文本表示), INTEGER (unix time)或REAL。 See: Datatypes in SQLite . 请参阅: SQLite中的数据类型

You can use leading zeroes (as dan suggested) or better yet, save them as UNIX timestamps in INTEGER fields. 您可以使用前导零(如建议的那样)或更好,将它们保存为INTEGER字段中的UNIX时间戳 You can convert these back to human-readable dates using SQLite's date and time functions or Java's (or whatever you are using). 您可以使用SQLite的日期和时间函数或Java(或您正在使用的任何内容)将这些日期转换回人类可读日期。

I have had to do similar sorting. 我不得不做类似的排序。 I ended up storing dates as REAL values in SQLite as the number of milliseconds since Epoch Jan 1 1970 00:00 GMT. 我最终在SQLite中将日期作为REAL值存储为自1970年1月1日00:00 GMT以来的毫秒数。 You can then simply sort these numerically (larger number occurred later chronologically). 然后,您可以简单地对这些数字进行排序(较大的数字后来按时间顺序排列)。 You can then convert to strings using the SimpleDateFormat. 然后,您可以使用SimpleDateFormat转换为字符串。 Also allows you to do great things with time elements which helps-- http://developer.android.com/reference/java/text/SimpleDateFormat.html 还可以让你用时间元素来做很棒的事情 - http://developer.android.com/reference/java/text/SimpleDateFormat.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用日期类获取格式为“ YYYY-MM-DD 00:00:00.0”的数据? - How can i get data in format “YYYY-MM-DD 00:00:00.0” using class Date? 如何将日期类型参数作为字符串放入 GET 请求中? 格式“yyyy-MM-dd” - How can I put Date type parameter as a String in GET request? Format "yyyy-MM-dd" 如何在yyyy-mm-dd中正确格式化日期? - How to properly format Date in yyyy-mm-dd? 我需要一个代码,该代码应使用Java从Excel获取YYYY-MM-DD格式的日期 - I need a code which should get Date from Excel as YYYY-MM-DD format using java 如何在我的选择框中列出日期格式为yyyy-mm-dd的月份值 - How can i list the month values in my select box with my date format yyyy-mm-dd 我怎样才能得到简单的 yyyy-MM-dd 这个日期? - How can i get simple yyyy-MM-dd out of this date? 如何将用户以MM-dd-yyyy格式选择的日期转换为Java中的yyyy-MM-dd格式 - How to convert a date a user selects in MM-dd-yyyy format to the format yyyy-MM-dd in Java 如何将包含日期的字符串值格式化为yyyy-mm-dd? - how to format a string value containing date to yyyy-mm-dd? 如何将日期转换为 yyyy-MM-dd 格式? - How to convert date in to yyyy-MM-dd Format? 如何初始化日期为YYYY-MM-DD格式的公历? - How to initialize Gregorian calendar with date as YYYY-MM-DD format?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM