iCalendar data files are plain text files with the extension .ics
iCalendar data has the MIME content type text/calendar.
Files with .ics extension which contain calendaring and scheduling information consistent with this MIME content type.
These calendar files allow different users to store calendar information within a text file .
Maven dependency
<dependency>
<groupId>org.mnode.ical4j</groupId>
<artifactId>ical4j</artifactId>
<version>1.0.4</version>
</dependency>
or can use
ical4j-1.0.4.jar
Program to Parse .ics file in java to fetch list of events with start date and end date
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.fortuna.ical4j.data.CalendarBuilder;
import net.fortuna.ical4j.model.Component;
import net.fortuna.ical4j.model.Property;
import net.fortuna.ical4j.model.property.CalScale;
import net.fortuna.ical4j.model.property.ProdId;
import net.fortuna.ical4j.model.property.Version;
public class ParseIcsFile {
public static void main(String args[]) throws Exception {
ParseIcsFile cusr = new ParseIcsFile();
File file = new File("Rohan_Calendar.ics");
String result = cusr.getCalInfoFromIcs(file);
System.out.println(result);
}
public String getCalInfoFromIcs(File file) throws Exception {
net.fortuna.ical4j.model.Calendar calendar = new net.fortuna.ical4j.model.Calendar();
calendar.getProperties().add(
new ProdId("-//Ben Fortuna//iCal4j 1.0//EN"));
calendar.getProperties().add(Version.VERSION_2_0);
calendar.getProperties().add(CalScale.GREGORIAN);
FileInputStream fin = new FileInputStream(file);
CalendarBuilder builder = new CalendarBuilder();
calendar = builder.build(fin);
List result = new ArrayList<String>();
for (Iterator i = calendar.getComponents().iterator(); i.hasNext();) {
Map<String, String> calenderinfo = new HashMap<String, String>();
Component component = (Component) i.next();
for (Iterator j = component.getProperties().iterator(); j.hasNext();) {
try {
String startdate = null, enddate = null, event = null;
Property property = (Property) j.next();
if ("DTSTART".equals(property.getName())) {
startdate = property.getValue();
calenderinfo.put("startDate",
modifyDateLayoutOfIcs(startdate));
}
if ("DTEND".equals(property.getName())) {
enddate = property.getValue();
calenderinfo.put("endDate",
modifyDateLayoutOfIcs(enddate));
}
if ("SUMMARY".equals(property.getName())) {
event = property.getValue();
calenderinfo.put("event", event);
}
if (!calenderinfo.isEmpty()) {
if (calenderinfo.get("event") != null) {
result.add(calenderinfo);
}
}
} catch (Exception ex) {
}
}
}
return result.toString();
}
private String modifyDateLayoutOfIcs(String inputDate) {
try {
SimpleDateFormat inDateFormat = new SimpleDateFormat("yyyyMMdd");
java.util.Date fromDate = inDateFormat.parse(inputDate);
SimpleDateFormat outDateForm = new SimpleDateFormat("dd-MMM-yyyy");
return outDateForm.format(fromDate);
} catch (Exception e) {
return inputDate;
}
}
}
iCalendar data has the MIME content type text/calendar.
Files with .ics extension which contain calendaring and scheduling information consistent with this MIME content type.
These calendar files allow different users to store calendar information within a text file .
Maven dependency
<dependency>
<groupId>org.mnode.ical4j</groupId>
<artifactId>ical4j</artifactId>
<version>1.0.4</version>
</dependency>
or can use
ical4j-1.0.4.jar
Program to Parse .ics file in java to fetch list of events with start date and end date
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.fortuna.ical4j.data.CalendarBuilder;
import net.fortuna.ical4j.model.Component;
import net.fortuna.ical4j.model.Property;
import net.fortuna.ical4j.model.property.CalScale;
import net.fortuna.ical4j.model.property.ProdId;
import net.fortuna.ical4j.model.property.Version;
public class ParseIcsFile {
public static void main(String args[]) throws Exception {
ParseIcsFile cusr = new ParseIcsFile();
File file = new File("Rohan_Calendar.ics");
String result = cusr.getCalInfoFromIcs(file);
System.out.println(result);
}
public String getCalInfoFromIcs(File file) throws Exception {
net.fortuna.ical4j.model.Calendar calendar = new net.fortuna.ical4j.model.Calendar();
calendar.getProperties().add(
new ProdId("-//Ben Fortuna//iCal4j 1.0//EN"));
calendar.getProperties().add(Version.VERSION_2_0);
calendar.getProperties().add(CalScale.GREGORIAN);
FileInputStream fin = new FileInputStream(file);
CalendarBuilder builder = new CalendarBuilder();
calendar = builder.build(fin);
List result = new ArrayList<String>();
for (Iterator i = calendar.getComponents().iterator(); i.hasNext();) {
Map<String, String> calenderinfo = new HashMap<String, String>();
Component component = (Component) i.next();
for (Iterator j = component.getProperties().iterator(); j.hasNext();) {
try {
String startdate = null, enddate = null, event = null;
Property property = (Property) j.next();
if ("DTSTART".equals(property.getName())) {
startdate = property.getValue();
calenderinfo.put("startDate",
modifyDateLayoutOfIcs(startdate));
}
if ("DTEND".equals(property.getName())) {
enddate = property.getValue();
calenderinfo.put("endDate",
modifyDateLayoutOfIcs(enddate));
}
if ("SUMMARY".equals(property.getName())) {
event = property.getValue();
calenderinfo.put("event", event);
}
if (!calenderinfo.isEmpty()) {
if (calenderinfo.get("event") != null) {
result.add(calenderinfo);
}
}
} catch (Exception ex) {
}
}
}
return result.toString();
}
private String modifyDateLayoutOfIcs(String inputDate) {
try {
SimpleDateFormat inDateFormat = new SimpleDateFormat("yyyyMMdd");
java.util.Date fromDate = inDateFormat.parse(inputDate);
SimpleDateFormat outDateForm = new SimpleDateFormat("dd-MMM-yyyy");
return outDateForm.format(fromDate);
} catch (Exception e) {
return inputDate;
}
}
}
Thank you for the code.
ReplyDeleteCan we use this code to parse outlook calendar ics files
this code can be used for any calendar .ics files
ReplyDelete