简体   繁体   English

Android以开始和结束时间开始日历意图

[英]Android Start calendar intent with start and end time

I have an app with an agenda with list of times and date. 我有一个应用程序,其中包含时间和日期列表的议程。 When user clicks one of these events, the Calendar intent should start so should bring up the Calendar with the time, date and reminder preset. 当用户单击其中一个事件时,应启动日历意图,因此应使用时间,日期和提醒预设调出日历。 However when I load up the intent, the start time is just the current time and the end time is an hour ahead. 但是,当我加载意图时,开始时间只是当前时间,结束时间是提前一小时。 Could someone please tell me what I am doing wrong. 有人可以告诉我我做错了什么。 Here is example of hard coded calendar with date and time set up I am testing with 这是我正在测试的日期和时间设置的硬编码日历的示例

Calendar startDate = Calendar.getInstance();
startDate.set(Calendar.MONTH, 8);
startDate.set(Calendar.YEAR, 2012);
startDate.set(Calendar.DAY_OF_MONTH, 5);
startDate.set(Calendar.HOUR_OF_DAY, 9);


// Set the calendar
Calendar endDate = Calendar.getInstance();
endDate.set(Calendar.MONTH, 8);
endDate.set(Calendar.YEAR, 2012);
endDate.set(Calendar.DAY_OF_MONTH, 5);
endDate.set(Calendar.HOUR_OF_DAY, 15);
endDate.set(Calendar.MINUTE, 30);

Intent intent = new Intent(Intent.ACTION_EDIT);
intent.putExtra("calendar_id", 1);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", startDate.getTime());
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("allDay", false);
intent.putExtra("endTime", endDate.getTime());
intent.putExtra("title", "A Test Event from android app");
intent.putExtra("description", "Your consulting date and time");
startActivity(intent);

After a lot of searching. 经过大量的搜索。 I found this method. 我发现了这种方法。 Bit annoying but it works 有点烦人,但它的工作原理

java.sql.Timestamp tsStart = java.sql.Timestamp.valueOf(year+ "-" + month + "-" + day + " " + hour + ":"+ minute + ":00");
java.sql.Timestamp tsStart = java.sql.Timestamp.valueOf(year+ "-" + month + "-" + day + " " + hour2+ ":"+ minute2+ ":00");

long startTime = tsStart.getTime();
long endTime = tsEnd.getTime();

I was just hoping I could use the Calendar method to set date and time, although guess doesn't really make a difference. 我只是希望我可以使用Calendar方法来设置日期和时间,尽管猜测并没有真正有所作为。 Also heads up to people using this method, make sure your year month day hour etc fields match up the the timestamp format. 同时使用此方法的人员,确保您的年月日等时间字段与时间戳格式匹配。 In my application, when I clicked on an agenda it would take the Date and time string of that event and then I was splitting it up like this 在我的应用程序中,当我点击议程时,它将采用该事件的日期和时间字符串,然后我将它拆分为这样

String array[] = tvSessionTime.getText().toString().split(" ");
String array2[] = array[0].split(":");
int iHour = Integer.parseInt(array2[0])
int sMinute = Integer.parseInt(array2[0])
String array3[] = tvSessionDate.getText().toString().split(" ");

And if you had a time like 07.05 and you parse the hour, the hour will come back as 7 and the minute would come back as 5. And the format is yyyy-hh-mm. 如果你有一个像07.05这样的时间并解析小时,则小时将返回为7,分钟将返回为5.并且格式为yyyy-hh-mm。 So this was bringing back an error as it didnt match the date format. 所以这会带来一个错误,因为它与日期格式不匹配。 Just a heads up! 只是一个抬头!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM