Java – Android starts the calendar with start and end times

I have an application that contains an agenda with a list of times and dates When the user clicks one of the events, the calendar intention shall be started, so the calendar shall be called up by using time, date and reminder presets However, when I load the intention, the start time is only the current time, and the end time is one hour earlier Someone can tell me what I did wrong This is an example of a hard coded calendar for the date and time settings I'm testing

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);

Solution

After a lot of searching I found this method It's a little 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 just wish I could use the calendar method to set the date and time, although speculation didn't really make a difference At the same time, the person using this method should ensure that your time fields such as month, year and day match the timestamp format In my application, when I click on the agenda, it takes the date and time string of the event, and then I split it into 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(" ");

If you have a time like 07.05 and parse the hours, the hours will be returned as 7 and the minutes will be returned as 5 And the format is yyyy HH mm So this will cause an error because it does not match the date format Just a look up!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>