2013-12-17 173 views
0

我想從我的應用程序中添加一個android日曆事件。我通過使用下面的代碼在日曆中添加了該事件。添加日曆事件

Calendar c = Calendar.getInstance(); 
date2 = formatter.parse(eDate); 
c.setTime(date2); 
c.add(Calendar.HOUR, 1); 
eDate = formatter.format(c.getTime()); 
date2 = formatter.parse(eDate);    
date2 = formatter.parse(eDate); 
c.setTime(date2); 
eDate = formatter.format(c.getTime()); 
cal1=Calendar.getInstance(); 
cal1.setTime(date1);   
cal2=Calendar.getInstance(); 
cal2.setTime(date2); 
calEvent.put("dtstart", cal1.getTimeInMillis()); 
calEvent.put("dtend", cal2.getTimeInMillis()); 
String timeZone = TimeZone.getDefault().getID(); 
calEvent.put("eventTimezone", timeZone); 
Uri uri = contentResolver.insert(Uri.parse("content://com.android.calendar/events"), 
calEvent); 
date2 = formatter.parse(eDate); 

我需要在日曆中添加一小時。因此,我已添加了1小時,結束日期,使用的代碼:

c.add(Calendar.HOUR, 1); 

但是當我看看日曆,它顯示12小時的活動。這意味着如果明天晚上10點增加了一個活動,它會在明天晚上10點到明天上午10點之間創建一個活動。

有人可以告訴我如何添加一個事件,明天晚上10點開始,明天晚上11點結束?

+0

嘗試蒙山此,calEvent。 putExtra(「endTime」,cal.getTimeInMillis()+ 60 * 1000); http://stackoverflow.com/questions/13268914/how-to-add-event-in-android-calendar-repeated-every-3-days – SolArabehety

回答

0

你應該檢查你沒有向我們顯示的格式。

更具體的,下面的代碼對我的作品:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd h a"); 
GregorianCalendar gcal = new GregorianCalendar(); 
gcal.set(Calendar.MILLISECOND, 0); 
gcal.set(2013, Calendar.DECEMBER, 18, 22, 0, 0); 
Date date1 = gcal.getTime(); 
System.out.println(formatter.format(date1)); // Output: 2013-12-18 10 PM 
gcal.add(Calendar.HOUR, 1); 
Date date2 = gcal.getTime(); 
System.out.println(formatter.format(date2)); // Output: 2013-12-18 11 PM 
0

你可以只加1小時(毫秒到您的結束時間), 像這樣

calEvent.put("dtend", calSet.getTimeInMillis() + 60 * 60 * 1000); 
相關問題