Java, ICS calendar format not showing time when imported in Outlook or Thunderbird

China☆狼群 提交于 2019-12-11 12:16:23

问题


I am working on a Java project in which I am creating an ICS file and there is something wrong with date and time. Whenever I import the ICS file, I want to show the startTime of event and endTime of Event in the ICS file.

I tried using the constructor which contains endTime as well, no luck, still shows 00:00. The below code I am using to generate an ICS file and below that is the content of the ICS file.

Code :

 Calendar icsCalendar = new Calendar();
            icsCalendar.getProperties().add(Version.VERSION_2_0);
            icsCalendar.getProperties().add(CalScale.GREGORIAN);

            String startDateString = new SimpleDateFormat("yyyyMMdd'T'hhmmss'Z'").format(groupNotes.getStartTimestamp());
            String endDateString = new SimpleDateFormat("yyyyMMdd'T'hhmmss'Z'").format(groupNotes.getEndTimestamp());
            net.fortuna.ical4j.model.Date startDt = null;
            net.fortuna.ical4j.model.Date endDateFortuna = null;
            try {
                startDt = new net.fortuna.ical4j.model.Date(startDateString, "yyyyMMdd'T'hhmmss'Z'");
                endDateFortuna = new net.fortuna.ical4j.model.Date(endDateString, "yyyyMMdd'T'hhmmss'Z'");
            } catch (ParseException e) {
                e.printStackTrace();
            }

            java.util.Calendar endDate = java.util.Calendar.getInstance();
            endDate.setTimeInMillis(groupNotes.getEndTimestamp().getTime());
           /* long difference = groupNotes.getEndTimestamp().getTime() - groupNotes.getStartTimestamp().getTime();
            int min = (int) (difference / (1000 * 60));
            Dur dur = new Dur(0, 0, min, 0);*/
            VEvent vEvent = new VEvent(startDt, endDateFortuna, groupNotes.getMnotetag());
            vEvent.getProperties().add(new Description());
            try {
                vEvent.getProperties().getProperty(Property.DESCRIPTION).setValue(groupNotes.getMnotetext());
                vEvent.getProperties().add(new Organizer("MAILTO:" + groupNotes.getNoteCreatorEmail()));

            } catch (IOException | URISyntaxException | ParseException e) {
                e.printStackTrace();
            }
            icsCalendar.getComponents().add(vEvent);

            FileOutputStream fout = null;

            try {
                fout = new FileOutputStream(calFile);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            CalendarOutputter outputter = new CalendarOutputter();
            outputter.setValidating(false);

            try {
                outputter.output(icsCalendar, fout);
                return new FileInputStream("mycalendar.ics");
            } catch (IOException | ValidationException e) {
                e.printStackTrace();
            }
        }

Here is how my ICS file looks like :

BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTAMP:20150910T152828Z
DTSTART;VALUE=DATE:20150911
DTEND;VALUE=DATE:20150911
SUMMARY:
DESCRIPTION:poip
ORGANIZER:MAILTO:email@gmail.com
END:VEVENT
END:VCALENDAR

Now when I import this, no time is mentioned in Outlook, thunderbird or Evolution. What am I doing wrong? Thank you.


回答1:


you are only specifying a DATE and not a DATE-TIME for the DTSTART and DTEND properties of your VEVENT.

Note: those are short for Date-Time Start and Date-Time End).

for more details you can refer to RFC5545 and specifically DATE-TIME, or DTSTART, and VEVENT.

The second one, also will remind you that the default for DTSTART is to use DATE-TIME format and not DATE format.

The third one indicates usage for DTSTART with only DATE value

The "VEVENT" is also the calendar component used to specify an anniversary or daily reminder within a calendar. These events have a DATE value type for the "DTSTART" property instead of the default value type of DATE-TIME.



来源:https://stackoverflow.com/questions/32506065/java-ics-calendar-format-not-showing-time-when-imported-in-outlook-or-thunderbi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!