Menu

Parse with a specific timezone

2014-03-21
2014-03-27
  • Caruyer Perrine

    Caruyer Perrine - 2014-03-21

    Hello,
    I'm really sorry, I hope you will forgive me, I'm back to speak about... timezones !

    First of all, thanks for your documentation "Working with Timezones", your explanations are very helpful !

    But there is something else I would like to be able to do :
    to parse with a specific timezone, and not with the JVM's one..

    Is there any way to do that without changing all events, one by one, and field by field ?

    Thanks for your help,

    Perrine

     
  • Michael Angstadt

    Hi Perrine!

    Ugh...timezones... ;)

    When biweekly parses the value of a date-based property, it attempts to determine the timezone that the date is in if a TZID parameter is defined. If it doesn't recognize the timezone (using the "java.util.TimeZone.getTimeZone(String)" method), then it just parses the date according to the JVM's default timezone.

    :::text
    "America/New_York" is recognized, so it parses the date according to that timezone
    DTSTART;TZID=America/New_York:20140322T120000
    
    timezone is UTC because of the "Z" on the end
    DTSTART:20140322T120000Z
    
    "foo" is not recognized, so it parses the date according to the default timezone
    DTSTART;TZID=foo:20140322T120000
    

    So, with the third example, knowing that it is parsed under the default timezone, you would have to manually convert the "java.util.Date" object to the timezone of your choosing.

    Does that help? Is there some feature that you'd like to see added?

    -Mike

     
  • Caruyer Perrine

    Caruyer Perrine - 2014-03-24

    Hello,
    thanks for your fast answer.

    In fact, the timezone in which I want to parse my iCal depends on the user who reads it, and must be managed dynamically.
    So, I have to change timezones on the fly.
    I quickly wrote this method :

    public static ICalendar parse(String calendarString, TimeZone timeZone){
        ICalendar iCalendar = Biweekly.parse(calendarString).first();
        for (VEvent vEvent : iCalendar.getEvents()){
            vEvent.getDateStart().setTimezoneId(timeZone.getID());
            vEvent.getDateEnd().setTimezoneId(timeZone.getID());
            for (RecurrenceDates date : vEvent.getRecurrenceDates()){
                date.setTimezoneId(timeZone.getID());
            }
        }
        return iCalendar;
    }
    

    Do you see some other date I could have forgot ?
    Maybe you can had something like this in your own implementations ;)

    Thanks for your help,
    Perrine

     
  • Michael Angstadt

    I'm confused about why you are setting the timezone ID. Calling "setTimezoneId()" has no effect on the property object's date value. The only time the timezone ID is used is when the iCal is written. When an iCal is written, it will format the date according to the property's timezone ID parameter.

    Mike

     
  • Caruyer Perrine

    Caruyer Perrine - 2014-03-27

    Hm, you're right, I'm sorry, I used to think it would set the date object TimeZone... But, the object is a Date and there is no timezones on Dates objects... really confused !
    I think I will take some more time to think about this, hoping I would'nt have to bother you again...

    Thanks for your patience ! :D
    And good luck for TimeZones!

     
  • Michael Angstadt

    Yes, Date objects don't have timezones associated with them. Their internal value is UTC time. You can format a Date object in a certain timezone by calling "DateFormat.setTimeZone()":

    ::::java
    Date date = new Date();
    TimeZone tz = TimeZone.getTimeZone("America/New_York");
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    df.setTimeZone(tz);
    System.out.println(df.format(date));
    
     

Anonymous
Anonymous

Add attachments
Cancel