簡體   English   中英

java.text.ParseException:無法解析的日期:指定時區 Australia/Lord_Howe 時 ical4j 中的“20221207T170935”

[英]java.text.ParseException: Unparseable date: "20221207T170935" in ical4j when specifying time zone Australia/Lord_Howe

我在嘗試以下代碼時遇到解析異常

    public class Timezone {

    public static void main(String[] args) {
        
        
        TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
        TimeZone tz;
        LocalDateTime now = LocalDateTime.now();
        final DateTimeFormatter ICS_DATE_FORMATTER =
                DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss");
        
        //tz = registry.getTimeZone("Asia/Calcutta");
        tz = registry.getTimeZone("Australia/Lord_Howe");
        DtStart dtstart;
        try {
        dtstart = new DtStart(now.format(ICS_DATE_FORMATTER),tz);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }    
    }
}

java.text.ParseException: Unparseable date: "20221207T170935"
    at java.base/java.text.DateFormat.parse(DateFormat.java:395)
    at net.fortuna.ical4j.model.DateTime.setTime(DateTime.java:418)
    at net.fortuna.ical4j.model.DateTime.<init>(DateTime.java:325)
    at net.fortuna.ical4j.model.property.DateProperty.setValue(DateProperty.java:137)
    at net.fortuna.ical4j.model.property.DtStart.<init>(DtStart.java:146)
    at Timezone.main(Timezone.java:33)

我使用了 ical4j 3.0.19 jar 及其依賴項 jar。

它適用於除“Australia/Lord_Howe”時區以外的所有時區。

我期望 Dtstart 值為:

DTSTART;TZID=澳大利亞/Lord_Howe:20221207T170935

無需解析:

package com.technojeeves.ical;

import java.util.List;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

import net.fortuna.ical4j.model.TimeZone;
import net.fortuna.ical4j.model.TimeZoneRegistry;
import net.fortuna.ical4j.model.TimeZoneRegistryFactory;
import net.fortuna.ical4j.model.property.DtStart;

import net.fortuna.ical4j.model.ParameterList;
import net.fortuna.ical4j.model.Parameter;

import net.fortuna.ical4j.model.parameter.TzId;

public class App {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        DtStart<LocalDateTime> localDtStart = new DtStart<>(now);
        System.out.println(localDtStart);
        ParameterList params = new ParameterList(List.of(new TzId("Australia/Lord_Howe")));
        DtStart<ZonedDateTime> zonedDtStart = new DtStart<>(params, ZonedDateTime.now());
        System.out.println(zonedDtStart);
    }
}

印刷

DTSTART:20221208T150844

DTSTART;TZID=Australia/Lord_Howe:20221209T020844

到目前為止,我還沒有得到我正在使用的版本 (4.0.0-beta4) 的源代碼。 是的,這是一個測試版,但我懷疑他們可能已經支持Temporal一段時間了。 實際上我發現所有版本 4.x 都支持它。

我的猜測是他們會及時修復上述 ctor 以將ZonedDateTime用作初始化類型,因為目前,如果使用它, DtStart.toString顯示區域的跡象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM