创建 ical 文件而不为 UTC 附加 Z

问题描述 投票:0回答:1

我有以下代码,成功生成ics文件。但我希望 DTSTART 在末尾附加 Z,因为它是 UTC,但这没有发生。

  public class IcalGenerator {

    public static void main(String[] args) {

        /* Create the event */
        String eventSummary = "New Year";
        LocalDateTime start = LocalDateTime.now(ZoneId.of("UTC"));
        VEvent event = new VEvent(start, Duration.of(30, ChronoUnit.MINUTES), eventSummary);
        event.add(new Clazz(Clazz.VALUE_PUBLIC));
        event.add(new Description("New meeting"));

        /* Create calendar */
        Calendar icsCalendar = new Calendar();
        var version = new Version();
        version.setValue(Version.VALUE_2_0);
        icsCalendar.add(version);

        // Set the location
        Location location = new Location("Conference Room");

        // Add the Location to the event
        event.add(location);

        /* Add event to calendar */
        icsCalendar.add(event);

        /* Create a file */
        String filePath = "my_meeting.ics";
        FileOutputStream out = null;
        try {

            out = new FileOutputStream(filePath);
            CalendarOutputter outputter = new CalendarOutputter();
            outputter.output(icsCalendar, out);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ValidationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

你能帮我解决我所缺少的吗?我最终想要 DTSTART:20231110T164148Z 而不是 DTSTART:20231110T164148

当我手动附加 Z 时效果很好。

java icalendar ical4j
1个回答
0
投票

以下是 ical4j 单元测试的一些示例:

        date                                                | expectedString
    LocalDate.of(2023, 11, 10)                          | 'DTSTART;VALUE=DATE:20231110\r\n'
    LocalDateTime.of(2023, 11, 10, 1, 1, 1)             | 'DTSTART:20231110T010101\r\n'
    LocalDateTime.of(2023, 11, 10, 1, 1, 1)
            .atZone(ZoneId.of('Australia/Melbourne'))   | 'DTSTART;TZID=Australia/Melbourne:20231110T010101\r\n'
    LocalDateTime.of(2023, 11, 10, 1, 1, 1)
            .toInstant(ZoneOffset.UTC)                  | 'DTSTART:20231110T010101Z\r\n'

目前 ical4j 中

LocalDateTime
被解释为浮动时间,因此您需要使用
Instant
来表示 UTC。

https://github.com/ical4j/ical4j/blob/674cc6860871f9fe6157a152ad2a4436824ada04/src/test/groovy/net/fortuna/ical4j/model/property/DtStartTestSpec.groovy#L58

© www.soinside.com 2019 - 2024. All rights reserved.