首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析CalendarContract.Events.DURATION值

解析CalendarContract.Events.DURATION值
EN

Stack Overflow用户
提问于 2015-06-17 11:53:51
回答 2查看 1.2K关注 0票数 1

我正在查询我的日历并显示所有事件的结果。当我试图解析值CalendarContract.Events.DURATION时,我得到了RFC2445字符串格式。

例如,我得到了"P15M“,我知道这是15分钟的会议。在互联网上挖掘了如何解析RFC2445之后,我找到了一些google 这里

但是有静态类来解析这些格式吗?编译一个jar并且只使用它一个函数--这不是很好.

谢谢你的帮助

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-18 14:19:51

我也遇到了和你一样的问题,在某个地方发现了以下有用的片段。我对它进行了一些重构,因此它将更加可读性。希望能帮上忙!

代码语言:javascript
复制
 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;
}
票数 4
EN

Stack Overflow用户

发布于 2015-06-17 12:45:21

游标的同时循环

字符串持续时间=“您的持续时间”;

代码语言:javascript
复制
        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块中解析即可。

我希望它能帮上忙

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30890829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档