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

239 Views Asked by At

I am getting a parsing exception while I am trying the following code

    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)

I have used ical4j 3.0.19 jar and their dependency jar .

Its working for all the timezone except for the timezone "Australia/Lord_Howe".

I am expecting the Dtstart value as :

DTSTART;TZID=Australia/Lord_Howe:20221207T170935

1

There are 1 best solutions below

3
g00se On

No need for parsing:

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);
    }
}

Prints

DTSTART:20221208T150844

DTSTART;TZID=Australia/Lord_Howe:20221209T020844

So far I haven't got the source of the version I'm using (4.0.0-beta4). Yes, it's a beta, but I suspect they might have supported Temporal for some time. Actually I discover that all versions 4.x support it.

My guess is that they'll fix the above ctor in time to act on ZonedDateTime as the initializing type, since, at the moment, if that's used, DtStart.toString shows no sign of the zone.