【问题标题】:Adding calendar event添加日历事件
【发布时间】:2013-12-17 17:46:59
【问题描述】:

我想从我的应用程序中在 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 点结束的活动?

【问题讨论】:

标签: java android events calendar


【解决方案1】:

您应该检查您没有向我们展示的格式模式。

更具体地说,以下代码对我有用:

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

【讨论】:

    【解决方案2】:

    您可以只增加 1 小时(结束时间的毫秒数), 像这样的东西

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多