How do I convert a string to an iCalendar(ics) object?
Question
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;
}
Solution
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;
}
OTHER TIPS
I suggest you try my new iCalendar API for Java called iCalendarFx. It can parse a string into any calendar element - VCALENDAR, VEVENT, etc.
You can check it out at http://jfxtras.org/
You can download it at https://github.com/JFXtras/jfxtras/tree/8.0/jfxtras-icalendarfx
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow