user2872856
user2872856

Reputation: 2051

Adding Calendar Event from WebView

I am following this tutorial to made adding of calendar event from WebView possible. I understand that the method is using shouldOverrideUrlLoading for url date:.

public boolean shouldOverrideUrlLoading (WebView view, String url) {

if (url.startsWith("date:")) {

Log.d(this.getClass().getCanonicalName(),url);

Calendar beginCal = Calendar.getInstance();

Calendar endCal = Calendar.getInstance();

Date beginDate = new Date(0, 0, 0);

Date endDate = new Date(0, 0, 0);

String parsed = url.substring(5);

String[] components = parsed.split(",");

beginDate.setMonth(Integer.parseInt(components[0]));

beginDate.setDate(Integer.parseInt(components[1]));

beginDate.setYear(Integer.parseInt(components[2]));

beginCal.setTime(beginDate);

endDate.setMonth(Integer.parseInt(components[3]));

endDate.setDate(Integer.parseInt(components[4]));

endDate.setYear(Integer.parseInt(components[5]));

endCal.setTime(endDate);

calendarevent(beginCal, endCal, components[6]);

return true;

}

return false;

}

});

However, I can't understand the format of date: url which I should put:

<a href='date:beginmonth, beginday, beginyear, 
endmonth, endday, endyear, My Event Description'>
My event link</a>

For example, I have an event on 15 September 2016, so I made the url become:

<a href='date:09,15,2016,09,15,2016, My Event Description'>
    My event link</a>

I got the wrong date. The date become 11 May 2011. What the mistake I made?

Upvotes: 1

Views: 972

Answers (1)

user2872856
user2872856

Reputation: 2051

Solved the problem myself. The bug occurs probably due to usage of deprecated Date as per documentation. Changed to Calendar.set and it's work now.

Calendar beginCal = Calendar.getInstance();
Calendar endCal = Calendar.getInstance();

String parsed = url.substring(5);
String[] components = parsed.split(",");
int month = Integer.parseInt(components[0])-1;
int day = Integer.parseInt(components[1]);
int year = Integer.parseInt(components[2]);
beginCal.set(year, month, day);

int monthend = Integer.parseInt(components[3])-1;
int dayend = Integer.parseInt(components[4]);
int yearend = Integer.parseInt(components[5]);

endCal.set(yearend, monthend, dayend);
calendarevent(beginCal, endCal, components[6]);

Upvotes: 1

Related Questions