2013-12-10 58 views
0

我正在我的日曆中創建事件到我的android設備。以下是我通過互聯網搜索到的代碼。添加日期和時間到事件日曆android

public void onClick(View arg0) { 
    long startTime = getMilliSeconds(startDate) + 1000 * 60 * 60; 
    long endTime = getMilliSeconds(endDate) + 1000 * 60 * 60 *2; 

    Intent intent = new Intent(Intent.ACTION_EDIT); 
    intent.setType("vnd.android.cursor.item/event"); 
    intent.putExtra("beginTime", startTime); 
    intent.putExtra("allDay", true); 
    intent.putExtra("endTime", endTime); 
    intent.putExtra("title", ""+mytitle); 
    startActivity(intent); 
} 

public long getMilliSeconds(String date) throws ParseException{ 
    Date dateSample = null ; 
    try { 
     //2013-12-28 23:30:00 
     SimpleDateFormat formatter ;  
     formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
     dateSample = (Date) formatter.parse(date); 
    } catch (Exception e) { 
    } 
    return dateSample.getTime(); 
} 

它工作正常,但創建事件時不顯示時間。僅顯示月份日期和年份。現在,我想在創建活動時展示時間。任何幫助將不勝感激。

+1

爲什麼一整天的活動都有開始時間?它的一整天。 – panini

回答

1

你將其設置爲全天:

intent.putExtra("allDay", true); 

如果去掉那行,它應該工作。

+0

hi @flx。謝謝您的回答。我也可以問「1000 * 60 * 60 * 2」是做什麼的?你的答案解決了我的問題,但不幸的是,我沒有得到正確的時間。我得到了正確的日期,但不是時間。謝謝 – kads

+0

'1000 * 60 * 60'是一個小時,以毫秒爲單位。 '* 2'使它兩個小時。 – flx