年从DatePickerDialog

年从DatePickerDialog

本文介绍了如何获得日,月,年从DatePickerDialog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得天,月,年值保存到数据库。这是我的codeS:

I'd like to get day, month and year values for save to db. These are my codes:

Declaretions:

Declaretions:

private TextView tv_purchase_date;
private Button mPickDate;
private int mYear;
private int mMonth;
private int mDay;
OnClickListener listener_show_dlg = null;
OnDateSetListener listener_mdate_display = null;

事件code:

listener_show_dlg = new OnClickListener() {
        public void onClick(View v) {
            Calendar cal=Calendar.getInstance();

            DatePickerDialog datePickDlg = new DatePickerDialog(
                    ItemsAddActivity.this,
                    listener_mdate_display,
                    cal.get(Calendar.YEAR),
                    cal.get(Calendar.MONTH),
                    cal.get(Calendar.DAY_OF_MONTH)
            );
            datePickDlg.show();
        };
    };


listener_mdate_display = new OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            mMonth = month;
                            mYear = year;
                            mDay = dayofMonth;

            tv_purchase_date.setText(dayOfMonth + "/" + monthOfYear + "/" + year);

        }
    };
}

我试图存储在数据库mMonth,mYear和MDAY值。什么是最好的存储类型?为整数或字符串??

I try to store mMonth, mYear and mDay values in db. What is the best store type? as integer or as string??

推荐答案

我存储在该重新presents日的DB一个数字。这是自现代时代的开始(1月1日,1970年)从日期选取器已通过秒数,你​​可以得到这样的并购ðY值:

I store in the DB one number that represents the date. It is the number of seconds that have passed since the beginning of the modern era (Jan 1, 1970.) From the Date Picker, you can get the M D Y values like this:

        datePickerListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int yearOfYear,
                int monthOfYear, int dayOfMonth) {
            // the user has picked these values
            year = yearOfYear;
            month = monthOfYear;
            day = dayOfMonth;

然后,我把这些成一个Date对象是这样的。

Then, I turn these into a single Date object like this.

                Calendar cal = new GregorianCalendar(year, month, day);
            Date dateOfGames = cal.getTime();
            DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
            String cs = df.format(dateOfGames);
            changeDateButton.setText(cs); // update the interface

        }
    };

我把它放在DB之前,我把它变成几秒钟这样的numebr:

before I put it in the DB, I turn it into a numebr of seconds like this:

长秒= date.getTime()/ 1000; //这是因为时代的开始。

long seconds = date.getTime() / 1000; // this is the date in seconds since the start of the epoch

...

当我走秒的DB是单号,并希望它再次成为一个Date对象,我这样做:

when I take that single number of seconds out of the DB, and want it to be a Date object again, I do this:

日期=新的日期(秒* 1000); //转换秒Date对象

date = new Date(seconds * 1000); // converting seconds to a Date object

您可以使用一个DateFormat对象来显示日期对象你怎么样才能看到它。

You can use a DateFormat object to display the date object how you like to see it.

我知道这是很尴尬。由于SQLite不允许你存储日期,答案将是尴尬。或许有比这更清洁的方式,和其他人将recommned东西。 :)

I know this is awkward. Since SQLite doesn't allow you to store a Date, the answer is going to be awkward. Perhaps there is a cleaner way than this, and others will recommned something. :)

这篇关于如何获得日,月,年从DatePickerDialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 22:30