我目前正在尝试使用我存储在我的数据库中的对象向Google calendar
添加活动。存储在数据库中的日期以日期格式存储,时间以字符串形式存储。通过意向向Google日历添加时间和日期
目前一切正在工作栏事件的时间部分。日期正确传递,但时间默认为0:00。我会如何做到这一点,所以时间过得恰当?
@Override
public void onClick(View arg0) {
if(arg0.getId() == R.id.btnAddToCal)
{
long startTime = 0, endTime=0;
String startDate = blankevent.EventDate;
try {
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
startTime=date.getTime();
}
catch(Exception e){ }
Calendar cal = Calendar.getInstance();
Intent calintent = new Intent(Intent.ACTION_EDIT);
calintent.setType("vnd.android.cursor.item/event");
calintent.putExtra("eventLocation", blankevent.EventLocation);
calintent.putExtra("title", blankevent.EventName);
calintent.putExtra("description", blankevent.EventDetails);
calintent.putExtra("beginTime",startTime);
calintent.putExtra("dtstart", blankevent.EventTime);
startActivity(calintent);
return;
}
我附上了实际问题区域的屏幕截图。日期是正确的,但我错误的时间。
解决代码
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.e("cal","sel2");
if(arg0.getId() == R.id.btnAddToCal)
{
String startDate = blankevent.EventDate;
String startHour = blankevent.EventTime;
Date fulldate = null;
try {
fulldate = new SimpleDateFormat("yyyy-MM-dd-HH:mm").parse(startDate+"-"+startHour);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
Intent calintent = new Intent(Intent.ACTION_EDIT);
calintent.setType("vnd.android.cursor.item/event");
calintent.putExtra("eventLocation", blankevent.EventLocation);
calintent.putExtra("title", blankevent.EventName);
calintent.putExtra("description", blankevent.EventDetails);
calintent.putExtra("beginTime", fulldate.getTime());
calintent.putExtra("endTime",fulldate.getTime()+60*60*1000);
startActivity(calintent);
return;
}
应该在哪里它会越来越'startTime'干净的例子吗?你的'SimpleDateFormat'解析器只会得到年,月和日。 – 2013-02-15 14:37:34
您的日期分析字符串错误,yyyy-mm-dd,表示年,月和日。应该是yyyy-MM-dd。 – Kennet 2013-02-15 14:41:11
startTime是EventDate的解析输出。 EventDate是在我的数据库中以YYYY-MM-DD格式存储的字段,我有一个以HH:MM形式存储为字符串的EventTime字段。 yyyy-MM-dd错误是我自己的错误,当输入时,它在实际代码中是正确的 – Andrela 2013-02-15 14:59:17