我正在关注this tutorial以便从WebView
添加日历活动。我了解该方法使用shouldOverrideUrlLoading
作为网址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;
}
});
但是,我无法理解date:
url的格式:
<a href='date:beginmonth, beginday, beginyear,
endmonth, endday, endyear, My Event Description'>
My event link</a>
例如,我在2016年9月15日举办了一个活动,所以我把网址变成了:
<a href='date:09,15,2016,09,15,2016, My Event Description'>
My event link</a>
我的日期错了。日期变成2011年5月11日。我犯了什么错误?
答案 0 :(得分:1)
自己解决了这个问题。该错误可能是由于documentation使用了已弃用的Date
。已更改为Calendar.set
,现在已正常工作。
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]);