我正在查询我的日历并显示所有事件的结果。当我试图解析值CalendarContract.Events.DURATION时,我得到了RFC2445字符串格式。
例如,我得到了"P15M“,我知道这是15分钟的会议。在互联网上挖掘了如何解析RFC2445之后,我找到了一些google 这里
但是有静态类来解析这些格式吗?编译一个jar并且只使用它一个函数--这不是很好.
谢谢你的帮助
发布于 2015-06-18 14:19:51
我也遇到了和你一样的问题,在某个地方发现了以下有用的片段。我对它进行了一些重构,因此它将更加可读性。希望能帮上忙!
public static long RFC2445ToMilliseconds(String str)
{
if(str == null || str.isEmpty())
throw new IllegalArgumentException("Null or empty RFC string");
int sign = 1;
int weeks = 0;
int days = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;
int len = str.length();
int index = 0;
char c;
c = str.charAt(0);
if (c == '-')
{
sign = -1;
index++;
}
else if (c == '+')
index++;
if (len < index)
return 0;
c = str.charAt(index);
if (c != 'P')
throw new IllegalArgumentException("Duration.parse(str='" + str + "') expected 'P' at index="+ index);
index++;
c = str.charAt(index);
if (c == 'T')
index++;
int n = 0;
for (; index < len; index++)
{
c = str.charAt(index);
if (c >= '0' && c <= '9')
{
n *= 10;
n += ((int)(c-'0'));
}
else if (c == 'W')
{
weeks = n;
n = 0;
}
else if (c == 'H')
{
hours = n;
n = 0;
}
else if (c == 'M')
{
minutes = n;
n = 0;
}
else if (c == 'S')
{
seconds = n;
n = 0;
}
else if (c == 'D')
{
days = n;
n = 0;
}
else if (c == 'T')
{
}
else
throw new IllegalArgumentException ("Duration.parse(str='" + str + "') unexpected char '" + c + "' at index=" + index);
}
long factor = 1000 * sign;
long result = factor * ((7*24*60*60*weeks)
+ (24*60*60*days)
+ (60*60*hours)
+ (60*minutes)
+ seconds);
return result;
}发布于 2015-06-17 12:45:21
游标的同时循环
字符串持续时间=“您的持续时间”;
try {
Duration d = new Duration();
d.parse(duration);
endMillis = startMillis + d.getMillis();
if (debug)
Log.d(TAG, "startMillis! " + startMillis);
if (debug)
Log.d(TAG, "endMillis! " + endMillis);
if (endMillis < startMillis) {
continue;
}
} catch (DateException e) {
if (debug)
Log.d(TAG, "duration:" + e.toString());
continue;
}课程持续时间为和也请参阅线路编号1487。有解析逻辑,您只需在源代码中包含工期类,并在try块中解析即可。
我希望它能帮上忙
https://stackoverflow.com/questions/30890829
复制相似问题