/** *将会议邀请信息生成标准格式的字符串 * @param startTime 开始时间 * @param endTime 结束时间 * @param title 会议名称 * @param organizerName 组织者名称 * @param organizerEmail 组织者Email * @param location 地点 * @param description 描述 * @param participatorNames 参与者名称列表,与participatorEmails一一对应 * @param participatorEmails 参与者Email列表 * @return */
public static String getICalText(Date startTime, Date endTime, String title, String organizerName, String organizerEmail, String location, String description, List participatorNames, List participatorEmails){
if(startTime != null && endTime != null && StringUtils.isNotBlank(organizerEmail) && participatorEmails != null && !participatorEmails.isEmpty() && participatorNames != null && participatorNames.size() == participatorEmails.size()) {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
String begin = format.format(startTime);
String over = format.format(endTime);
// 创建一个时区(TimeZone)
TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
TimeZone timezone = registry.getTimeZone("PRC");
VTimeZone tz = timezone.getVTimeZone();
//起始时间
java.util.Calendar startDate = java.util.Calendar.getInstance(timezone);
//月份从0开始
startDate.set(java.util.Calendar.MONTH, Integer.parseInt(begin.toString().split("-")[1].trim()) - 1);
startDate.set(java.util.Calendar.DAY_OF_MONTH, Integer.parseInt(begin.toString().split("-")[2].trim()));
startDate.set(java.util.Calendar.YEAR, Integer.parseInt(begin.toString().split("-")[0].trim()));
startDate.set(java.util.Calendar.HOUR_OF_DAY, Integer.parseInt(begin.toString().split("-")[3].trim()));
startDate.set(java.util.Calendar.MINUTE, Integer.parseInt(begin.toString().split("-")[4].trim()));
startDate.set(java.util.Calendar.SECOND, Integer.parseInt(begin.toString().split("-")[5].trim()));
//结束时间
java.util.Calendar endDate = java.util.Calendar.getInstance(timezone);
endDate.set(java.util.Calendar.MONTH, Integer.parseInt(over.toString().split("-")[1].trim()) - 1);
endDate.set(java.util.Calendar.DAY_OF_MONTH, Integer.parseInt(over.toString().split("-")[2].trim()));
endDate.set(java.util.Calendar.YEAR, Integer.parseInt(over.toString().split("-")[0].trim()));
endDate.set(java.util.Calendar.HOUR_OF_DAY, Integer.parseInt(over.toString().split("-")[3].trim()));
endDate.set(java.util.Calendar.MINUTE, Integer.parseInt(over.toString().split("-")[4].trim()));
endDate.set(java.util.Calendar.SECOND, Integer.parseInt(over.toString().split("-")[5].trim()));
// 创建事件
DateTime start = new DateTime(startDate.getTime());
DateTime end = new DateTime(endDate.getTime());
String eventTitle = "";
if(StringUtils.isNotBlank(title)){
eventTitle = title;
}
VEvent meeting = new VEvent(start, end, eventTitle);
// 添加时区信息
meeting.getProperties().add(tz.getTimeZoneId());
// 生成唯一标志符
UidGenerator ug = new UidGenerator("uidGen");
Uid uid = ug.generateUid();
meeting.getProperties().add(uid);
// 组织者
Organizer organizer = new Organizer(URI.create("mailto:" + organizerEmail));
organizer.getParameters().add(new Cn(organizerName));
meeting.getProperties().add(organizer);
if(StringUtils.isNotBlank(location)) {
meeting.getProperties().add(new Location(location));
}
if(StringUtils.isNotBlank(description)) {
meeting.getProperties().add(new Description(description));
}
// 添加参加者 .
for (int i = 0; i < participatorEmails.size(); i++) {
Attendee dev = new Attendee(URI.create("mailto:" + participatorEmails.get(i)));
dev.getParameters().add(Role.REQ_PARTICIPANT);
dev.getParameters().add(new Cn(participatorNames.get(i)));
meeting.getProperties().add(dev);
}
// 创建日历
Calendar icsCalendar = new Calendar();
icsCalendar.getProperties().add(new ProdId("-//Events Calendar//iCal4j 1.0//EN"));
icsCalendar.getProperties().add(Version.VERSION_2_0);
icsCalendar.getProperties().add(Method.REQUEST);
icsCalendar.getProperties().add(CalScale.GREGORIAN);
// 添加事件
icsCalendar.getComponents().add(meeting);
CalendarOutputter outputter = new CalendarOutputter();
Writer writer = new StringWriter();
outputter.output(icsCalendar, writer);
return writer.toString();
} catch (Exception e) {
LOG.error(e.getMessage());
}
}
return "";
}