0
在我的應用程序的一部分中,用戶需要選擇和開始和結束事件的時間,所以我顯示兩個連續的選擇器,以便用戶可以選擇開始/結束的日期/時間,但問題是總是會在最終結果中添加一天。DateTime選取器對話框將添加一天到我選擇的日期
這裏是我的代碼:
private Date FechaInicio, FechaFin;
private Calendar myCalendar = Calendar.getInstance();
private Calendar myCalendarEnd = Calendar.getInstance();
private long epochTimeStart, epochTimeEnd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nuevo_xxxxx_step3);
//First Date picjer that have the listener dateStart
new DatePickerDialog(xxxxx.this, dateStart, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DATE)).show();
}
//Start date listener
DatePickerDialog.OnDateSetListener dateStart = new DatePickerDialog.OnDateSetListener(){
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DATE, dayOfMonth);
//Start de time picker for startdate
new TimePickerDialog(xxxxx.this,timeStart,myCalendar.get(Calendar.HOUR),myCalendar.get(Calendar.MINUTE),false).show();
}
};
//Time picker dialog for starTime
TimePickerDialog.OnTimeSetListener timeStart = new TimePickerDialog.OnTimeSetListener(){
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
myCalendar.set(Calendar.HOUR, hourOfDay);
myCalendar.set(Calendar.MINUTE,minute);
epochTimeStart = myCalendar.getTimeInMillis();
//Start de DatePicker for End Date with
new DatePickerDialog(xxxxxx.this, dateEnd, myCalendarEnd
.get(Calendar.YEAR), myCalendarEnd.get(Calendar.MONTH),
myCalendarEnd.get(Calendar.DATE)).show();
}
};
//Listener for end date
DatePickerDialog.OnDateSetListener dateEnd = new DatePickerDialog.OnDateSetListener(){
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
//Save the date in other calendar instance
myCalendarEnd.set(Calendar.YEAR, year);
myCalendarEnd.set(Calendar.MONTH, monthOfYear);
myCalendarEnd.set(Calendar.DATE, dayOfMonth);
//Time picker for end date
new TimePickerDialog(xxx.this,timeEnd,myCalendarEnd.get(Calendar.HOUR),myCalendarEnd.get(Calendar.MINUTE),false).show();
}
};
//Listener for time of End Date
TimePickerDialog.OnTimeSetListener timeEnd = new TimePickerDialog.OnTimeSetListener(){
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
myCalendarEnd.set(Calendar.HOUR, hourOfDay);
myCalendarEnd.set(Calendar.MINUTE,minute);
epochTimeEnd = myCalendarEnd.getTimeInMillis();
}
}
我對時間變量epochTimeStart
,epochTimeEnd
,但是當我將它們轉換爲日期是一直在增加一天(我嘗試轉換日期上的Web www.epochconverter .com和我有相同的結果)。
數據「當我將其轉換爲日期」裏? –
在其他活動的另一種方法(我沒有發佈所有的代碼),但轉換不是問題,因爲我打印紀元時間,並使用在線轉換器(如www.epochconverter.com),結果是相同的,問題在於計算時代 – user3515652
其中是「計算時代」?你對計算有什麼期望?最後要顯示的數據是什麼? –