添加日历事件

问题描述:

我想从我的应用程序中添加一个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://*.com/questions/13268914/how-to-add-event-in-android-calendar-repeated-every-3-days – SolArabehety

你应该检查你没有向我们显示的格式。

更具体的,下面的代码对我的作品:

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 

你可以只加1小时(毫秒到您的结束时间), 像这样

calEvent.put("dtend", calSet.getTimeInMillis() + 60 * 60 * 1000);