Getting correct dates with timezone

Help
Anonymous
2012-10-18
2013-06-05
  • Anonymous - 2012-10-18

    Hello,

    I'm parsing my ics file (generated by outlook), which has the timezone information as follows:

    BEGIN:VTIMEZONE
    TZID:Środkowoeuropejski czas stand.
    BEGIN:STANDARD
    DTSTART:16011028T030000
    RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
    TZOFFSETFROM:+0200
    TZOFFSETTO:+0100
    END:STANDARD
    BEGIN:DAYLIGHT
    DTSTART:16010325T020000
    RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3
    TZOFFSETFROM:+0100
    TZOFFSETTO:+0200
    END:DAYLIGHT
    END:VTIMEZONE
    

    And some event:

    BEGIN:VEVENT
    CLASS:PUBLIC
    CREATED:20121010T062739Z
    DTEND:20121012T110000Z
    DTSTAMP:20121012T140818Z
    DTSTART:20121012T090000Z
    LAST-MODIFIED:20121012T083342Z
    PRIORITY:5
    SEQUENCE:0
    SUMMARY:Spotkanie projekty
    TRANSP:TRANSPARENT
    UID:AAAAAJV2gPBI86RLlAqE5/C5PUlEozMA
    END:VEVENT
    

    According to Outlook, the event starts at 11:00 but after parsing the file in my app, the event starts at 09:00. Here's the code:

    CalendarBuilder builder = new CalendarBuilder();
    Calendar calendar = builder.build(new FileInputStream("ical.ics"));
    ComponentList cl = calendar.getComponents(Component.VEVENT);
    for (Object o : cl) {
        Component component = (Component) o;
        DtStart dtStartProp = (DtStart) component.getProperty(Property.DTSTART);
        
        System.println("tz=" + dtStartProp.getDate()); // output: 20121012T090000Z
    }
    

    Where do I add the timezone information?

    Jakub

     
  • Ben Fortuna

    Ben Fortuna - 2012-10-18

    Hi Jakub,

    In your event there isn't any reference to the timezone, and in fact the DTSTART is specified in UTC. Effectively this is still correct (9am UTC is the same as 11am local time), but probably not what you were expecting from outlook. Probably a better output would have something like this:

    BEGIN:VEVENT
    CLASS:PUBLIC
    CREATED:20121010T062739Z
    DTEND;TZID="Środkowoeuropejski czas stand.":20121012T130000
    DTSTAMP:20121012T140818Z
    DTSTART;TZID="Środkowoeuropejski czas stand.":20121012T110000
    LAST-MODIFIED:20121012T083342Z
    PRIORITY:5
    SEQUENCE:0
    SUMMARY:Spotkanie projekty
    TRANSP:TRANSPARENT
    UID:AAAAAJV2gPBI86RLlAqE5/C5PUlEozMA
    END:VEVENT
    

    Nevertheless you can still use the date you are provided, you just need to convert it to local time:

    java.util.Calendar cal = Calendar.getInstance();
    cal.setTime(dtStartProp.getDate());
    Date localTime = cal.getTime();
    

    You can also modify the object model to add the TZID parameter to the relevant event properties, but depending on your use case that may be overkill.

    regards,
    ben

     
  • Ben Fortuna

    Ben Fortuna - 2012-10-18

    Also note you don't even really have to convert the date, as the original object is still the same time, the only difference is the toString() method is overridden to display in UTC time..

     
  • Anonymous - 2012-10-19

    Also note you don't even really have to convert the date, as the original object is still the same time

    Indeed, thanks for your hints.

    I formetted the date with:

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.println(formatter.format(dtStartProp.getDate())); // output: 2012-10-12 11:00:00
    

    which is the date I expected in the first place.

    Thanks and regards

     

Log in to post a comment.