2012-05-02 72 views
2

我嘗試添加以下日期和時間在我的Android日曆事件....添加android系統中的日曆事件的日期和時間

Title and description = Events for test 
Start Date = May 05, 2012, 07:10PM 
End Date = May 06, 2012, 02:10PM 

以下是我的代碼添加到事件

    long startTime = 0, endTime = 0; 
        Date d = new Date(); 
      startTime = d.parse(start); 
      d.setTime(startTime); 

      endTime = d.parse(end); 
      d.setTime(endTime); 

      Intent intent = new Intent(Intent.ACTION_EDIT); 
      intent.setType("vnd.android.cursor.item/event"); 
      intent.putExtra(Events.TITLE, summary); 
      intent.putExtra(Events.DESCRIPTION, summary); 
      intent.putExtra(Events.EVENT_LOCATION, ""); 
      intent.putExtra(Events.DTSTART, startTime); 
      intent.putExtra(Events.DTEND, endTime); 
      intent.putExtra(Events.ALL_DAY, allDayFlag); 
      intent.putExtra(Events.STATUS, 1); 
      intent.putExtra(Events.VISIBLE, 0); 
      intent.putExtra(Events.HAS_ALARM, 1); 
      startActivity(intent); 

但是在活動詳情頁面,我得到了標題,說明僅僅是正確的。在開始和結束日期的地方,我將獲得當前日期,其中開始和結束時間是下一個1小時的時間。

誰能說我什麼是我的代碼去錯了.....

+0

也許,如果您的開始日期和結束日期是unixtime而不是任何的電話設置爲?. – Warpzit

回答

7

如果你所得到的數據如下Date = May 05, 2012, 07:10PM,然後將其轉換日期爲數字格式五月是05和下面的代碼傳遞值。

Calendar beginCal = Calendar.getInstance(); 
       beginCal.set(year, mnth, day, hrs, min); 
       startTime = beginCal.getTimeInMillis(); 

       Calendar endCal = Calendar.getInstance(); 
       endCal.set(year, mnth, day, hrs, min); 
       endTime = endCal.getTimeInMillis();  

       Intent intent = new Intent(Intent.ACTION_INSERT); 
       intent.setType("vnd.android.cursor.item/event"); 
       intent.putExtra(Events.TITLE, summary); 
       intent.putExtra(Events.DESCRIPTION, summary); 
       intent.putExtra(Events.EVENT_LOCATION, "");  
       intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginCal.getTimeInMillis()); 
       intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endCal.getTimeInMillis()); 
       intent.putExtra(Events.ALL_DAY, allDayFlag); 
       intent.putExtra(Events.STATUS, 1); 
       intent.putExtra(Events.VISIBLE, 0); 
       intent.putExtra(Events.HAS_ALARM, 1); 
       startActivity(intent); 
+3

好吧,它的工作原理,但任何人都可以解釋爲什麼Events.DTSTART和Events.DTEND不起作用? –

0

我覺得可能是你的日期解析導致此問題。

試試這個

DateFormat formatter = new SimpleDateFormat("MMMM dd, yyyy, h:mmaa"); 
long lnsTime, lneTime; 

     Date dateObject; 

     try{ 
     String dob_var = "May 05, 2012, 07:10PM"; 

     dateObject = formatter.parse(dob_var); 

     lnsTime = dateObject.getTime(); 
     Log.e(null, Long.toString(lnsTime)); 

     dob_var = "May 06, 2012, 02:10PM";  

     dateObject = formatter.parse(dob_var); 

     lneTime = dateObject.getTime(); 
     Log.e(null, Long.toString(lneTime)); 
     } 

     catch (java.text.ParseException e) 
      { 
      // TODO Auto-generated catch block 
       e.printStackTrace(); 
       Log.i("E11111111111", e.toString()); 
      } 

    Intent intent = new Intent(Intent.ACTION_EDIT); 
      intent.setType("vnd.android.cursor.item/event"); 
      intent.putExtra(Events.TITLE, summary); 
      intent.putExtra(Events.DESCRIPTION, summary); 
      intent.putExtra(Events.EVENT_LOCATION, ""); 
      intent.putExtra(Events.DTSTART, lnsTime); 
      intent.putExtra(Events.DTEND, lneTime); 
      intent.putExtra(Events.ALL_DAY, allDayFlag); 
      intent.putExtra(Events.STATUS, 1); 
      intent.putExtra(Events.VISIBLE, 0); 
      intent.putExtra(Events.HAS_ALARM, 1); 
      startActivity(intent); 

希望這會幫助你。

謝謝...

+0

它返回E11111111111(2541):java.text.ParseException:不可解析日期:「2012年5月11日,10:42 PM」(在偏移量6),並且我的問題繼續... –

+0

請確保您導入這些:' import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date;' –

+0

@Siva K,更改SimpleDateFormat(「MMMM dd,yyyy,h:mm aa」);.這將解決您的問題。因爲你在10:42 PM有SPACE。 –

2

不是傳遞的意圖,我們還可以添加事件到日曆這樣的,這看起來很酷

公共靜態字符串getCalendarUriBase(){

String calendarUriBase = null; 
    Uri calendars = Uri.parse("content://calendar/calendars"); 
    Cursor managedCursor = null; 
    try { 
     managedCursor = act.managedQuery(calendars, null, null, null, null); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    if (managedCursor != null) { 
     calendarUriBase = "content://calendar/"; 
    } else { 
     calendars = Uri.parse("content://com.android.calendar/calendars"); 
     try { 
      managedCursor = act.managedQuery(calendars, null, null, null, 
        null); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     if (managedCursor != null) { 
      calendarUriBase = "content://com.android.calendar/"; 
     } 
    } 
    // //Log.d("Calendar", "" + calendarUriBase); 
    return calendarUriBase; 
} 

main() { 

    DateFormat formatter = new SimpleDateFormat("MMMM dd, yyyy, h:mma"); 
    String dateStarts = "May 05, 2012, 07:10PM"; 
    String dateEnds = "May 06, 2012, 02:10PM"; 
    Uri EVENTS_URI = Uri.parse(getCalendarUriBase() + "events"); 
    ContentResolver cr = getActivity().getContentResolver(); 
    // event insert 
    ContentValues values = new ContentValues(); 
    Date startDate, endDate; 
    startDate = formatter.parse(dateStarts); 
    endDate = formatter.parse(dateEnds); 
    values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID()); 
    values.put("calendar_id", 1); 
    values.put("title", remainderTitle); 
    values.put("allDay", 0); 
    values.put("dtstart", startDate.getTime()); 
    values.put("dtend", enddDate.getTime()); 
    values.put("description", remainderInfo); 
    values.put("visibility", 0); 
    values.put("hasAlarm", 1);// now 
    try { 
     cr.insert(EVENTS_URI, values); 
     // reminder insert 
     Uri REMINDERS_URI = Uri.parse(getCalendarUriBase() 
       + "reminders"); 
     values = new ContentValues(); 
     values.put("event_id", 1); 
     values.put("method", 1); 
     values.put("minutes", 10); 
     cr.insert(REMINDERS_URI, values); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

希望,它有一定幫助的你:)

謝謝

2

它可能是android SDK問題,請試試這個

int SDK_INT = android.os.Build.VERSION.SDK_INT; 
    DateFormat formatter = new SimpleDateFormat("MMMM dd, yyyy, h:mmaa"); 
    long lnsTime = 0, lneTime = 0; 

    Date dateObject; 

    try { 
     String dob_var = "May 05, 2012, 07:10PM"; 

     dateObject = formatter.parse(dob_var); 

     lnsTime = dateObject.getTime(); 
     Log.e(null, Long.toString(lnsTime)); 

     dob_var = "May 06, 2012, 02:10PM"; 

     dateObject = formatter.parse(dob_var); 

     lneTime = dateObject.getTime(); 
     Log.e(null, Long.toString(lneTime)); 
    } 

    catch (java.text.ParseException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     Log.i("E11111111111", e.toString()); 
    } 

    Intent intent = new Intent(Intent.ACTION_EDIT); 
    intent.setType("vnd.android.cursor.item/event"); 
    if (SDK_INT >= 15) { 
     intent.putExtra(Events.CALENDAR_ID, 1); 
     intent.putExtra(Events.TITLE, "Title"); 
     intent.putExtra(Events.DESCRIPTION, "Title"); 
     intent.putExtra(Events.DTSTART, lnsTime); 
     intent.putExtra(Events.DTEND, lneTime); 
     intent.putExtra(Events.ALL_DAY, 0); 
     intent.putExtra(Events.STATUS, 1); 
     intent.putExtra(Events.VISIBLE, 0); 
     intent.putExtra(Events.HAS_ALARM, 1); 
    } else { 
     intent.putExtra("calendar_id", 1); 
     intent.putExtra("status", 1); 
     intent.putExtra("visibility", 0); 
     intent.putExtra("hasAlarm", 1); 
     intent.putExtra("beginTime", lnsTime); 
     intent.putExtra("allDay", true); 
     intent.putExtra("rrule", "FREQ=YEARLY"); 
     intent.putExtra("endTime", lneTime); 
     intent.putExtra("title", "Title"); 
     intent.putExtra("description", "Title"); 
    } 
    startActivity(intent); 

希望它可以幫助你:)

感謝