2013-11-25 48 views
3

我想在android中獲取日曆事件的開始時間和結束時間?我如何存檔它?如何從android中的日曆事件獲取開始時間和結束時間?

這是我的代碼,

public static void readCalendarEvent(Context context) throws ParseException { 
     ContentResolver contentResolver = context.getContentResolver(); 
     Calendar calendar = Calendar.getInstance(); 
     String dtstart = "dtstart"; 
     String dtend = "dtend"; 


     SimpleDateFormat displayFormatter = new SimpleDateFormat(" MMMM dd, yyyy (EEEE)"); 

     stime=displayFormatter.format(calendar.getTime());  

     SimpleDateFormat startFormatter = new SimpleDateFormat("MM/dd/yy"); 
     String dateString = startFormatter.format(calendar.getTime()); 

     long after = calendar.getTimeInMillis(); 
     SimpleDateFormat formatterr = new SimpleDateFormat("hh:mm:ss MM/dd/yy"); 
     Calendar endOfDay = Calendar.getInstance(); 
     Date dateCCC = formatterr.parse("47:59:59 " + dateString); 
     endOfDay.setTime(dateCCC); 



     cursor = contentResolver.query(Uri.parse("content://com.android.calendar/events"), (new String[] { "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation" }), "(" + dtstart + ">" + after + " and " + dtend + "<" + endOfDay.getTimeInMillis() + ")", null, "dtstart ASC"); 
     gCalendar = new ArrayList<GoogleCalendar>(); 
     try { 
      System.out.println("Count=" + cursor.getCount()); 

      if (cursor.getCount() > 0) { 

       System.out.println("the control is just inside of the cursor.count loop"); 
       while (cursor.moveToNext()) { 
        GoogleCalendar googleCalendar = new GoogleCalendar(); 
        gCalendar.add(googleCalendar); 
        int calendar_id = cursor.getInt(0); 
        googleCalendar.setCalendar_id(calendar_id); 
        String title = cursor.getString(1); 
        googleCalendar.setTitle(title); 
        String description = cursor.getString(2); 
        googleCalendar.setDescription(description); 
        String dtstart1 = cursor.getString(3); 
        googleCalendar.setDtstart(dtstart1);      
        System.out.println("Starting date is :" +dtstart1); 
        String dtend1 = cursor.getString(4); 
        googleCalendar.setDtend(dtend1); 
        String eventlocation = cursor.getString(5); 
        googleCalendar.setEventlocation(eventlocation); 
       } 
      } 
     } catch (AssertionError ex) { 
      ex.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

的代碼工作正常,但我想從這個code.I開始時間結束時間我能實現嗎?

+0

任何答案都會真的有幫助嗎? – user2841300

+0

我得到這樣的結束日期(結束日期是:-1881036608) – Deepak

+0

你是什麼意思開始和結束時間?你有他們的字符串表示。 – windchime

回答

0

Android的日曆事件以毫秒爲單位給出開始或結束日期(1970年1月1日)。所以我複製了某人的代碼片段以將毫秒轉換爲日期和時間。該功能是

public static String getDate(long milliSeconds) { 
      SimpleDateFormat formatter = new SimpleDateFormat(
        "dd/MM/yyyy hh:mm:ss a"); 
      Calendar calendar = Calendar.getInstance(); 
      calendar.setTimeInMillis(milliSeconds); 
      return formatter.format(calendar.getTime()); 


} 

然後我這樣稱呼它獲取日期時間值,其中我執行

Log.d("start date", getDate(Long.parseLong(cursor.getString(4)))); 

希望這有助於。

+0

一個方便的網站,如果你想轉換時間http://www.ruddwire.com/handy-code/date-to-millisecond-calculators/ –

相關問題