2011-08-04 23 views
0

这是使用webview从页面上的按钮调用的。除了传递时间之外,它很好用。我究竟做错了什么?在日期时间传递给android日历时失败(主要为@ Oriharel)

public void addToCalendar(final long startTime, String allday, final long endTime, String title, String myLocation) { 
      // TODO Auto-generated method stub    
      Calendar cal = Calendar.getInstance();    
       Intent intent = new Intent(Intent.ACTION_EDIT); 
       intent.setType("vnd.android.cursor.item/event");      
       intent.putExtra("beginTime", cal.startTime); 
       intent.putExtra("allDay", allday); 
       intent.putExtra("rrule", "FREQ=YEARLY"); 
       intent.putExtra("endTime", cal.endTime); 
       intent.putExtra("title", title); 
       intent.putExtra("eventLocation",myLocation); 
       startActivity(intent); 
     } 

我收到日期1970年1月15日10时45分和1970年1月15日10时45分,当我试图传递

How do I pass the date time? 

这主要是@Oriharel发布How to add calendar events in Android?

+0

不这段代码给出'intent.putExtra( “BEGINTIME”,cal.startTime)语法错误;'?在这篇文章中的开始时间是从1970年1月1日开始的毫秒数。 –

+0

那么这就是我刚才intent.putExtra(“beginTime”,startTime);不过,我的日期和时间刚刚出来,所有的时髦。 – Rick

回答

1

好,我得到它的工作。

public void addToCalendar(String startTime, String endTime, String allDay, String title, String myLocation, String descript) { 
      // TODO Auto-generated method stub 

      java.sql.Timestamp ts1 = java.sql.Timestamp.valueOf(startTime); 
      java.sql.Timestamp ts2 = java.sql.Timestamp.valueOf(endTime); 

      long tsTime1 = ts1.getTime(); 
      long tsTime2 = ts2.getTime(); 

       Intent intent = new Intent(Intent.ACTION_EDIT); 
       intent.setType("vnd.android.cursor.item/event"); 

       intent.putExtra("beginTime", tsTime1); 
       intent.putExtra("allDay", allDay); 
       intent.putExtra("rrule", "FREQ=YEARLY"); 
       intent.putExtra("endTime", tsTime2); 
       intent.putExtra("title", title); 
       intent.putExtra("eventLocation",myLocation); 
       intent.putExtra("description", descript); 
       startActivity(intent); 
     } 
+1

嗯,我很高兴我可以帮助。 – Snicolas

0

你如何使用参数startTime和endTime?其实你现在使用一个新的日历实例。

问候, 斯特凡

+0

“指向现在”===这是什么意思?这里有链接吗?我看到新的API http://code.google.com/apis/calendar/data/2.0/developers_guide_java.html#CreatingSingle但仍然...仍然如何通过日期时间我看不到格式。 – Rick

+0

我只是说你没有使用方法的参数,而是使用了新的日历对象。无论如何,什么是startTime和endTime,你如何得到他们的价值? – Snicolas

+0

我通过webview中的一个javascript包装器传递它们。现在我试着“2012-08-01T14:00:00” – Rick

0

发布替代代码作为援助他人如何做到这一点。我已经对日期字符串进行了硬编码,使示例更加清晰。

private long getEventDateAsLong(String dateToProcess) { 

    Date date = parseDate(dateToProcess); 
    Timestamp ts = new Timestamp(date.getTime()); 

    return ts.getTime(); 

} 

private Date parseDate(String date) throws ParseException {  

    DateFormat formatter = DateFormat.getDateInstance(); 

     try { 
    return formatter.parse(date); 
    } catch (java.text.ParseException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

正如我刚才所说,我已经硬编码的两个例子日期为字符串,所以你可以看到它是如何工作的:

String startDateToProcess = "July 3, 2013"; 
String endDateToProcess = "July 15, 2013"; 

,然后无论你正在创建日历的意图,你的代码看起来应该是这样的:

Intent intent = new Intent(Intent.ACTION_EDIT); 
intent.setType("vnd.android.cursor.item/event"); 
intent.putExtra("beginTime", getEventDateAsLong(startDateToProcess)); 
intent.putExtra("endTime", getEventDateAsLong(endDateToProcess)); 
startActivity(intent); 
+0

我的开始时间:10/2/2016是在这种格式...怎么办? – Elizabeth