Java 添加日历事件

Java 添加日历事件,java,android,events,calendar,Java,Android,Events,Calendar,我想从我的应用程序在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 = fo

我想从我的应用程序在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小时的活动。这意味着,如果a添加了一个明天晚上10点的事件,它将创建一个从明天晚上10点到明天上午10点的事件


有谁能告诉我如何添加一个从明天晚上10点开始到明天晚上11点结束的活动吗?

您应该检查您尚未显示给我们的格式模式

更具体地说,以下代码适用于我:

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);   

试试这个,calEvent.putExtra(“endTime”,cal.getTimeInMillis()+60*1000);