解析 CalendarContract.Events.DURATION 个值

Parse CalendarContract.Events.DURATION values

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

例如,我得到 "P15M",我知道这是 15 分钟的会议。 在网上搜索如何解析 RFC2445 后,我发现了一些 google jar here

但是是否有任何静态 class 来解析这些格式? 编译一个 jar 并仅将其用于 1 个函数,这不是很好...

感谢帮助

在游标的while循环中

String duration = "your duration";

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

The class Duration is and also refer 行号 1487。有解析逻辑,您只需要在源代码中包含持续时间 class 并像在 try 块中一样解析。

希望对你有帮助

我遇到了和你一样的问题,四处寻找并在某处找到了以下有用的片段。我对它进行了一些重构,因此它会更具可读性。希望对您有所帮助!

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