-->

How to get day, month and year from DatePickerDial

2019-07-23 07:48发布

问题:

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

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;

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

        }
    };
}

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

回答1:

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;

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

        }
    };

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

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

....

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

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

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

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. :)



回答2:

I struggled with this issue for a while. I don't konw of anything better than this.

I stored the date in the DB as a single long int. It is pretty easy to convert your Date to the number of seconds since the epoch (Jan 1, 1970), and it is also easy to convert the number of seconds into a Date object.

You need to be careful with seconds and milliseconds.

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

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

// Use Greg calendar to get a Date object from day, month, year

Date dateOfGames = new GregorianCalendar(year, month, day).getTime();

Does that help at all?



回答3:

I created sqllite table with this sql string:

create table items (id INTEGER PRIMARY KEY AUTOINCREMENT, pdate DATE)

I writed some methods to convert date:

public String date_to_str(Date date) {      
    String pattern = "dd.MM.yyyy";
    SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
    Log.d(_watcher_name, "date_to_str" + dateFormat.format(date));
    return dateFormat.format(date);     
}

public Date mdy_to_date (int day, int month, int year) {
    Calendar cal = new GregorianCalendar(year, month, day);
    return cal.getTime();       
}