我目前正在尝试使用我存储在数据库中的对象向Google calendar
添加一个事件。存储在数据库中的日期以Date格式存储,时间存储为字符串。
目前一切正常,事件的时间部分。日期正确传递,但时间默认为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;
}
答案 0 :(得分:2)
你的开始时间需要几毫秒。此外,我真的没有额外的字段名称。
由于您只解析dd - MM - YYYY,时间仍然是00:00,事件的默认持续时间是1小时。 所以你可能只需要设置Date对象的时间(startTime),然后调用startTime.getMillis();
String startDate = blankevent.EventDate;
String startHour = blankevent.EventTime;
Date date = new SimpleDateFormat("yyyy-MM-dd-HH:mm").parse(startDate+"-"+startHour);
另外, 以下是www.developer.com By Lauren Darcey & Shane Conder
的简洁示例Intent calIntent = new Intent(Intent.ACTION_INSERT);
calIntent.setData(Events.CONTENT_URI);
calIntent.putExtra(Events.TITLE, "Google IO Afterparty");
calIntent.putExtra(Events.EVENT_LOCATION, "The W Hotel Bar on Third Street");
calIntent.putExtra(Events.DESCRIPTION, "Hang out after Google IO for a drink and geeky conversation.");
Calendar startTime = Calendar.getInstance();
startTime.set(2012, 5, 29, 18, 0);
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 5, 29, 22, 30);
calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
startTime.getTimeInMillis());
calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
endTime.getTimeInMillis());
startActivity(calIntent);