安卓先从开始和结束时间的日历意图 [英] Android Start calendar intent with start and end time

查看:125
本文介绍了安卓先从开始和结束时间的日历意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有随着时代和日期列表中的议程中的应用程序。当用户点击其中一个事件,日历意图应该开始所以应该带了时间,日期和提醒preSET日历。然而,当我加载的目的,开始时间就是当前时间和结束时间提前半小时。可能有人请告诉我,我做错了。下面是例子硬codeD日历日期和时间设置,我有测试

 日历的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);


//设置日历
日历结束日期= 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.ACTION_EDIT);
intent.putExtra(CALENDAR_ID,1);
intent.setType(vnd.android.cursor.item /事件);
intent.putExtra(BEGINTIME,startDate.getTime());
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra(allDay,假);
intent.putExtra(endTime的,endDate.getTime());
intent.putExtra(标题,一个测试赛,从Android应用程序);
intent.putExtra(说明,你的咨询日期和时间);
startActivity(意向);
 

解决方案

很多搜索之后。我发现这个方法。有点讨厌,但它的工作原理

 的java.sql.Timestamp tsStart = java.sql.Timestamp.valueOf(年+ - +月+ - +日++小时+:+分钟+:00);
的java.sql.Timestamp tsStart = java.sql.Timestamp.valueOf(年+ - +月+ - +日++ hour2 +:+ minute2 +:00);

长的startTime = tsStart.getTime();
长endTime的= tsEnd.getTime();
 

我只是希望我可以用日历的方法来设置日期和时间,但猜测并没有真正发挥作用。同时抬起头,使用这种方法的人,请确保您的年,月,日,小时等领域相匹配的时间戳格式。在我的应用程序,当我点击了一个议程,将采取该事件的日期和时间的字符串,然后我劈开它像这样

  String数组[] = tvSessionTime.getText()的toString()分裂()。;
字符串ARRAY2 [] =阵列[0] .split(:);
INT iHour =的Integer.parseInt(ARRAY2 [0])
INT sMinute =的Integer.parseInt(ARRAY2 [0])
字符串ARRAY3 [] = tvSessionDate.getText()的toString()分裂()。;
 

如果你有时间像07.05和你解析小时,小时会回来为7分钟会回来为5,格式为yyyy-HH-毫米。因此,这被带回一个错误,因为它没有匹配的日期格式。只是抬起头来!

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. 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. So this was bringing back an error as it didnt match the date format. Just a heads up!

这篇关于安卓先从开始和结束时间的日历意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆