CodePrimate CodePrimate - 1 month ago 12
Java Question

How do I convert a string to an iCalendar(ics) object?

I have been given the task of implementing a way to convert a string received through a JSON object to an iCalendar object(ics). I found the iCal4j library and have been attempting to use that as my parser. however it seems that the CalendarBuilder takes an InputStream.

How do I proceed ?

String response = jsonObj.getString("icalendar");

CalendarBuilder calBuiler = new CalendarBuilder();
Calendar calendar = calBuilder.build("???");
....


Edit : Would this work?

public Calendar convertStringtoCalendar(String arg)
{
CalendarBuilder calBuiler = new CalendarBuilder();
InputStream is;
try {
is = new ByteArrayInputStream(arg.getBytes("UTF-8"));
return calBuiler.build(is);

} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

Answer

Solved the problem by doing the following.

public static Component getCalendarEvent(String myCalendarString) 
{
    try {
        StringReader sin = new StringReader(myCalendarString);
        CalendarBuilder builder = new CalendarBuilder();
        Calendar calendar = builder.build(sin);

        return (Component)calendar.getComponent("VEVENT");

    } catch (Exception e) {e.printStackTrace();}

    return null;
}
Comments