Parse CalendarContract.Events.DURATION values

1.3k views Asked by At

I'm querying my calendar and showing all events result. When i'm trying to parse the value CalendarContract.Events.DURATION i'm getting a RFC2445 string format.

For example i'm getting "P15M" which I know it's 15 minutes meeting. After digging on the internet on how to parse RFC2445 I've found some google jar here

But is there any static class to parse those format? Compiling a jar and using it only for 1 function it's not that nice...

Thanks for the help

2

There are 2 answers

0
Eyal Perry On BEST ANSWER

I have had the same problem as you, dug around and found the following useful snippet somewhere. I refactored it a bit, so it will be more readable. hope it helps!

 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;
}
0
Rahul Patil On

In while loop of cursor

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 line number 1487. there is parsing logic , you just need to include duration class in your source code and parse as in try block.

I hope it helps