Android DatePicker每月关闭一个?

时间:2011-05-13 19:13:19

标签: java android date datepicker timepicker

我正在尝试使用datepicker和timepicker对话框创建一个unix时间戳,但出现的问题是这一天总是被一个人关闭。例如。如果我选择5月10日它将显示5月11日作为输出。然而其他一切似乎都没有问题。任何建议都会很棒。

日期和时间选择器代码如下所示......

private DatePickerDialog.OnDateSetListener mDateSetListener =
    new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, 
                              int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
        }
    };

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        mHour = hourOfDay;
        mMinute = minute;
        confirmDateChange();
    }
};

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this,
                    mDateSetListener,
                    mYear, mMonth, mDay);
    case TIME_DIALOG_ID:
    return new TimePickerDialog(this,
            mTimeSetListener, mHour, mMinute, false);          
    }
    return null;
}

接下来我调用它来获取时间戳

 private long componentTimeToTimestamp(int year, int month, int day, int hour, int minute) {

    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DAY_OF_MONTH, day);
    c.set(Calendar.HOUR, hour);
    c.set(Calendar.MINUTE, minute);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);       
    return (c.getTimeInMillis()/1000L);        
}

最后,这会在textview上设置值,但就像我说的那个月的日期一直是一个

    long mydate = componentTimeToTimestamp(mYear, mMonth, mDay, mHour, mMinute);   
    long datemod = mydate * 1000;
    final java.util.Date d = new java.util.Date(datemod);       
    details.setText(d.toLocaleString());

1 个答案:

答案 0 :(得分:2)

虽然你在评论中找到了答案。但是你必须在这里回答它,因为大多数人都不会混淆它没有得到答案。请阅读评论然后回答这个答案

 c.set(year, month, day, hour, minute, 0);