Android DatePicker 一个月中的哪一天减一?
我正在尝试使用日期选择器和时间选择器对话框创建 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);
}
最后这会在文本视图中设置值,但就像我说的那样,该月的某一天总是相差一位
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());
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 DatePickerDialog(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());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然你在评论中找到了答案。但是你一定已经在这里回答了,因为大多数人不会混淆它还没有得到回答。请阅读评论,然后寻找这个答案
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