Android DatePicker day of month off by one?
I'm trying to create a unix timestamp using the datepicker and timepicker dialogs but the problem occurring is that the day is always off by one. E.g. if I pick May 10 it will display May 11 as the output. Everything else however seems to work no problem. Any suggestions would be great.
The date and timepicker code looks as follows...
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 DatePicker开发者_如何学GoDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, false);
}
return null;
}
Next I call this to get a timestamp
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);
}
And lastly this sets the value at a textview but like I said the day of the month is always off by one
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());
Though You figured out the answer in comment.But u must have answered it here as most of the people wont confuse that it has not not been answered.Please read the comments and then go for this answer
c.set(year, month, day, hour, minute, 0);
精彩评论