2017-01-10 74 views
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(); 
} 

} 

我對時間變量epochTimeStartepochTimeEnd,但是當我將它們轉換爲日期是一直在增加一天(我嘗試轉換日期上的Web www.epochconverter .com和我有相同的結果)。

+0

數據「當我將其轉換爲日期」裏? –

+0

在其他活動的另一種方法(我沒有發佈所有的代碼),但轉換不是問題,因爲我打印紀元時間,並使用在線轉換器(如www.epochconverter.com),結果是相同的,問題在於計算時代 – user3515652

+0

其中是「計算時代」?你對計算有什麼期望?最後要顯示的數據是什麼? –

回答

0

我去槽你的代碼,它工作正常。在這裏我用它來檢查你選擇的日期和時間。 https://currentmillis.com/

enter image description here

看到有兩個時間和日期UTC之一,當地one.You需要使用當地的一個,因爲揮霍不貼你轉換的方式,如果你有時間和日期的差異嘗試轉換它以正確的格式。

裏面你第一次選擇器

epochTimeStart = myCalendar.getTimeInMillis(); 

      Calendar cl = Calendar.getInstance(); 
      cl.setTimeInMillis(epochTimeStart); 
      cl.add(Calendar.MONTH,1); 
      String date = "" + cl.get(Calendar.DAY_OF_MONTH) + ":" + cl.get(Calendar.MONTH) + ":" + cl.get(Calendar.YEAR); 
      String time = "" + cl.get(Calendar.HOUR_OF_DAY) + ":" + cl.get(Calendar.MINUTE) + ":" + cl.get(Calendar.SECOND); 

      Log.d("TimeCheck *****",date + " "+ time); 

,它會回報你這樣4:5:2017 16:49:38

相關問題